成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

mongo + express + ng2 + nodejs tasklist

187J3X1 / 835人閱讀

摘要:準(zhǔn)備目錄開始后端創(chuàng)建項(xiàng)目前端

準(zhǔn)備

nodejs

mongo

目錄
|-client
    |-app
        |-components
            |-task
                |-tasks.component.html
                |-tasks.components.ts
            |-services
                |-task.services.ts
            |-app.component.html
            |-app.component.ts
            |-app.module.ts
            |-main.ts
        |-bower_components
        |-node_modules
        |-typings
        |-package.json
        |-systemjs.config.js
        |-Task.ts
        |-tsconfig.json
        |-typings.json
    |-node_modules
    |-routes
        |-index.js
        |-tasks.js
    |-views
        |-index.html
    |-.bowerrc
    |-package.json
    |-server.js
開始 后端

cd tasklist && npm init #創(chuàng)建項(xiàng)目

npm install mongojs express ejs body-parser --save

touch server.js

    var express = require("express")
    var path = require("path")
    var bodyParser = require("body-parser")

    var index = require("./routes/index");
    var tasks = require("./routes/tasks");

    var port = 3000;
    var app = express();

    app.set("views", path.join(__dirname, "views"));
    app.set("view engine", "ejs");
    app.engine("html", require("ejs").renderFile);

    app.use(express.static(path.join(__dirname, "client")));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));


    app.use("/", index);
    app.use("/api", tasks);

    app.listen(port, function () {
        console.log("Server started on port" + port)
    });

mkdir routes && cd routes

touch index.js

    var express = require("express")
    var router = express.Router();

    router.get("/", function (req, res, next) {
        res.render("index.html");
    });
    module.exports = router;

mkdir views && cd views && touch index.html


    
    
        
        Title
    
    
        HELLO WORLD
    

touch tasks.js

var express = require("express")
var router = express.Router();
var mongojs = require("mongojs")
var db = mongojs("mongodb://localhost:27017/nodeapp", ["tasks"])

router.get("/tasks", function (req, res, next) {
    db.tasks.find(function (err, tasks) {
        if (err) {
            res.send(err);
        }
        res.json(tasks);
    });
});

router.get("/task/:id", function (req, res, next) {
    db.tasks.findOne({_id: mongojs.ObjectId(req.params.id)}, function (err, task) {
        if (err) {
            res.send(err);
        }
        res.json(task);
    })
});

router.post("/task", function (req, res, next) {
    var task = req.body;
    if (!task.title || !(task.isDone + "")) {
        req.status(400);
        res.json({
            "error": "Bad Date"
        });
    } else {
        db.tasks.save(task, function (err, task) {
            if (err) {
                res.send(err);
            }
            res.json(task);
        });
    }
});

router.delete("/task/:id", function (req, res, next) {
    db.tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function (err, task) {
        if (err) {
            res.send(err);
        }
        res.json(task);
    })
})

router.put("/task/:id", function (req, res, next) {
    var task = req.body;
    var updTask = {};

    if (task.isDone) {
        updTask.isDone = task.isDone;
    }
    if (task.title) {
        updTask.title = task.title;
    }
    if(!updTask) {
        res.status(400);
        res.json({
            "error": "Bad Data"
        })
    } else {
        db.tasks.update({_id: mongojs.ObjectId(req.params.id)}, updTask, function (err, task) {
            if (err) {
                res.send(err);
            }
            res.json(task);
        });
    }
})

module.exports = router;
前端

mkdir client && mkdir app

touch package.json

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently "tsc -w" "lite-server" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.0",
    "@angular/compiler": "~2.1.0",
    "@angular/core": "~2.1.0",
    "@angular/forms": "~2.1.0",
    "@angular/http": "~2.1.0",
    "@angular/platform-browser": "~2.1.0",
    "@angular/platform-browser-dynamic": "~2.1.0",
    "@angular/router": "~3.1.0",
    "@angular/upgrade": "~2.1.0",
    "angular-in-memory-web-api": "~0.1.5",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3",
    "typings":"^1.4.0"
  }
}

touch typings.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160909174046"
  }
}

touch tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false
    }
}

systemjs.config.js

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      "npm:": "node_modules/"
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: "app",
      // angular bundles
      "@angular/core": "npm:@angular/core/bundles/core.umd.js",
      "@angular/common": "npm:@angular/common/bundles/common.umd.js",
      "@angular/compiler": "npm:@angular/compiler/bundles/compiler.umd.js",
      "@angular/platform-browser": "npm:@angular/platform-browser/bundles/platform-browser.umd.js",
      "@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js",
      "@angular/http": "npm:@angular/http/bundles/http.umd.js",
      "@angular/router": "npm:@angular/router/bundles/router.umd.js",
      "@angular/forms": "npm:@angular/forms/bundles/forms.umd.js",
      // other libraries
      "rxjs":                      "npm:rxjs",
      "angular-in-memory-web-api": "npm:angular-in-memory-web-api",
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: "./main.js",
        defaultExtension: "js"
      },
      rxjs: {
        defaultExtension: "js"
      },
      "angular-in-memory-web-api": {
        main: "./index.js",
        defaultExtension: "js"
      }
    }
  });
})(this);

npm install

touch .bowerrc

{
  "directory": "./client/bower_components"
}

bower install bootstrap --save

Demo

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/18894.html

相關(guān)文章

  • mongo + express + ng2 + nodejs tasklist

    摘要:準(zhǔn)備目錄開始后端創(chuàng)建項(xiàng)目前端 準(zhǔn)備 nodejs mongo 目錄 |-client |-app |-components |-task |-tasks.component.html |-tasks.components.ts |-services...

    chanjarster 評論0 收藏0
  • Express 實(shí)戰(zhàn)(八):利用 MongoDB 進(jìn)行數(shù)據(jù)持久化

    摘要:在使用過程中我們可以通過增加哈希次數(shù)來提高數(shù)據(jù)的安全性。當(dāng)然,對密碼的哈希操作應(yīng)該在保存數(shù)據(jù)之前。 showImg(https://segmentfault.com/img/remote/1460000010821081); 毫無疑問,幾乎所有的應(yīng)用都會(huì)涉及到數(shù)據(jù)存儲。但是 Express 框架本身只能通過程序變量來保存數(shù)據(jù),它并不提供數(shù)據(jù)持久化功能。而僅僅通過內(nèi)存來保存數(shù)據(jù)是無法應(yīng)對...

    yanbingyun1990 評論0 收藏0
  • 公司項(xiàng)目NODEJS實(shí)踐0.3[ mongo / session ...]

    摘要:使用,可參考執(zhí)行退出命令,只要設(shè)置,即可。下節(jié)主要實(shí)現(xiàn)注冊時(shí)的郵件驗(yàn)證保存登錄狀態(tài)異常處理,同步異步操作全棧工程技術(shù)新群上一篇公司項(xiàng)目實(shí)踐下一篇待續(xù) 一、前言 ???書接上回,我們搭建了WEB服務(wù)端路由、模板等功能,完成了register 通過ajax與后端的通信,今天主要完成數(shù)據(jù)與mongodb的存取,實(shí)現(xiàn)注冊 / 登錄 / 退出功能 ???DEMO GIT https://gi...

    MoAir 評論0 收藏0
  • MEVN 架構(gòu)(MongoDB + Express + Vue + NODEJS)搭建

    摘要:連接數(shù)據(jù)庫如果不自己創(chuàng)建默認(rèn)數(shù)據(jù)庫會(huì)自動(dòng)生成地址跟第一步的地址對應(yīng)?,F(xiàn)在回過頭來看里面的入口文件最后,我們在瀏覽器輸入,就會(huì)跳到。到此為止,我們就完成了整個(gè)前后端各自開發(fā)到正式部署的流程。 一個(gè)完整的網(wǎng)站服務(wù)架構(gòu)包括:1、web frame ---這里應(yīng)用express框架2、web server ---這里應(yīng)用nodejs3、Database ---這里應(yīng)用monggoDB4、...

    Lsnsh 評論0 收藏0

發(fā)表評論

0條評論

187J3X1

|高級講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<