资讯专栏INFORMATION COLUMN

mongo + express + ng2 + nodejs tasklist

chanjarster / 885人阅读

摘要:准备目录开始后端创建项目前端

准备

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 #创建项目

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/91043.html

相关文章

  • mongo + express + ng2 + nodejs tasklist

    摘要:准备目录开始后端创建项目前端 准备 nodejs mongo 目录 |-client |-app |-components |-task |-tasks.component.html |-tasks.components.ts |-services...

    187J3X1 评论0 收藏0
  • Express 实战(八):利用 MongoDB 进行数据持久化

    摘要:在使用过程中我们可以通过增加哈希次数来提高数据的安全性。当然,对密码的哈希操作应该在保存数据之前。 showImg(https://segmentfault.com/img/remote/1460000010821081); 毫无疑问,几乎所有的应用都会涉及到数据存储。但是 Express 框架本身只能通过程序变量来保存数据,它并不提供数据持久化功能。而仅仅通过内存来保存数据是无法应对...

    yanbingyun1990 评论0 收藏0
  • 公司项目NODEJS实践0.3[ mongo / session ...]

    摘要:使用,可参考执行退出命令,只要设置,即可。下节主要实现注册时的邮件验证保存登录状态异常处理,同步异步操作全栈工程技术新群上一篇公司项目实践下一篇待续 一、前言 ⋅⋅⋅书接上回,我们搭建了WEB服务端路由、模板等功能,完成了register 通过ajax与后端的通信,今天主要完成数据与mongodb的存取,实现注册 / 登录 / 退出功能 ⋅⋅⋅DEMO GIT https://gi...

    MoAir 评论0 收藏0
  • MEVN 架构(MongoDB + Express + Vue + NODEJS)搭建

    摘要:连接数据库如果不自己创建默认数据库会自动生成地址跟第一步的地址对应。现在回过头来看里面的入口文件最后,我们在浏览器输入,就会跳到。到此为止,我们就完成了整个前后端各自开发到正式部署的流程。 一个完整的网站服务架构包括:1、web frame ---这里应用express框架2、web server ---这里应用nodejs3、Database ---这里应用monggoDB4、...

    Lsnsh 评论0 收藏0

发表评论

0条评论

chanjarster

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<