资讯专栏INFORMATION COLUMN

mongo + express + ng2 + nodejs tasklist

187J3X1 / 881人阅读

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

准备

nodejs

mongo

目录

</>复制代码

  1. |-client
  2. |-app
  3. |-components
  4. |-task
  5. |-tasks.component.html
  6. |-tasks.components.ts
  7. |-services
  8. |-task.services.ts
  9. |-app.component.html
  10. |-app.component.ts
  11. |-app.module.ts
  12. |-main.ts
  13. |-bower_components
  14. |-node_modules
  15. |-typings
  16. |-package.json
  17. |-systemjs.config.js
  18. |-Task.ts
  19. |-tsconfig.json
  20. |-typings.json
  21. |-node_modules
  22. |-routes
  23. |-index.js
  24. |-tasks.js
  25. |-views
  26. |-index.html
  27. |-.bowerrc
  28. |-package.json
  29. |-server.js
开始 后端

cd tasklist && npm init #创建项目

npm install mongojs express ejs body-parser --save

touch server.js

</>复制代码

  1. var express = require("express")
  2. var path = require("path")
  3. var bodyParser = require("body-parser")
  4. var index = require("./routes/index");
  5. var tasks = require("./routes/tasks");
  6. var port = 3000;
  7. var app = express();
  8. app.set("views", path.join(__dirname, "views"));
  9. app.set("view engine", "ejs");
  10. app.engine("html", require("ejs").renderFile);
  11. app.use(express.static(path.join(__dirname, "client")));
  12. app.use(bodyParser.json());
  13. app.use(bodyParser.urlencoded({extended: false}));
  14. app.use("/", index);
  15. app.use("/api", tasks);
  16. app.listen(port, function () {
  17. console.log("Server started on port" + port)
  18. });

mkdir routes && cd routes

touch index.js

</>复制代码

  1. var express = require("express")
  2. var router = express.Router();
  3. router.get("/", function (req, res, next) {
  4. res.render("index.html");
  5. });
  6. module.exports = router;

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

</>复制代码

  1. Title
  2. HELLO WORLD

touch tasks.js

</>复制代码

  1. var express = require("express")
  2. var router = express.Router();
  3. var mongojs = require("mongojs")
  4. var db = mongojs("mongodb://localhost:27017/nodeapp", ["tasks"])
  5. router.get("/tasks", function (req, res, next) {
  6. db.tasks.find(function (err, tasks) {
  7. if (err) {
  8. res.send(err);
  9. }
  10. res.json(tasks);
  11. });
  12. });
  13. router.get("/task/:id", function (req, res, next) {
  14. db.tasks.findOne({_id: mongojs.ObjectId(req.params.id)}, function (err, task) {
  15. if (err) {
  16. res.send(err);
  17. }
  18. res.json(task);
  19. })
  20. });
  21. router.post("/task", function (req, res, next) {
  22. var task = req.body;
  23. if (!task.title || !(task.isDone + "")) {
  24. req.status(400);
  25. res.json({
  26. "error": "Bad Date"
  27. });
  28. } else {
  29. db.tasks.save(task, function (err, task) {
  30. if (err) {
  31. res.send(err);
  32. }
  33. res.json(task);
  34. });
  35. }
  36. });
  37. router.delete("/task/:id", function (req, res, next) {
  38. db.tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function (err, task) {
  39. if (err) {
  40. res.send(err);
  41. }
  42. res.json(task);
  43. })
  44. })
  45. router.put("/task/:id", function (req, res, next) {
  46. var task = req.body;
  47. var updTask = {};
  48. if (task.isDone) {
  49. updTask.isDone = task.isDone;
  50. }
  51. if (task.title) {
  52. updTask.title = task.title;
  53. }
  54. if(!updTask) {
  55. res.status(400);
  56. res.json({
  57. "error": "Bad Data"
  58. })
  59. } else {
  60. db.tasks.update({_id: mongojs.ObjectId(req.params.id)}, updTask, function (err, task) {
  61. if (err) {
  62. res.send(err);
  63. }
  64. res.json(task);
  65. });
  66. }
  67. })
  68. module.exports = router;
前端

mkdir client && mkdir app

touch package.json

</>复制代码

  1. {
  2. "name": "angular-quickstart",
  3. "version": "1.0.0",
  4. "scripts": {
  5. "start": "tsc && concurrently "tsc -w" "lite-server" ",
  6. "lite": "lite-server",
  7. "postinstall": "typings install",
  8. "tsc": "tsc",
  9. "tsc:w": "tsc -w",
  10. "typings": "typings"
  11. },
  12. "licenses": [
  13. {
  14. "type": "MIT",
  15. "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
  16. }
  17. ],
  18. "dependencies": {
  19. "@angular/common": "~2.1.0",
  20. "@angular/compiler": "~2.1.0",
  21. "@angular/core": "~2.1.0",
  22. "@angular/forms": "~2.1.0",
  23. "@angular/http": "~2.1.0",
  24. "@angular/platform-browser": "~2.1.0",
  25. "@angular/platform-browser-dynamic": "~2.1.0",
  26. "@angular/router": "~3.1.0",
  27. "@angular/upgrade": "~2.1.0",
  28. "angular-in-memory-web-api": "~0.1.5",
  29. "bootstrap": "^3.3.7",
  30. "core-js": "^2.4.1",
  31. "reflect-metadata": "^0.1.8",
  32. "rxjs": "5.0.0-beta.12",
  33. "systemjs": "0.19.39",
  34. "zone.js": "^0.6.25"
  35. },
  36. "devDependencies": {
  37. "concurrently": "^3.0.0",
  38. "lite-server": "^2.2.2",
  39. "typescript": "^2.0.3",
  40. "typings":"^1.4.0"
  41. }
  42. }

touch typings.json

</>复制代码

  1. {
  2. "globalDependencies": {
  3. "core-js": "registry:dt/core-js#0.0.0+20160725163759",
  4. "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
  5. "node": "registry:dt/node#6.0.0+20160909174046"
  6. }
  7. }

touch tsconfig.json

</>复制代码

  1. {
  2. "compilerOptions": {
  3. "target": "es5",
  4. "module": "commonjs",
  5. "moduleResolution": "node",
  6. "sourceMap": true,
  7. "emitDecoratorMetadata": true,
  8. "experimentalDecorators": true,
  9. "removeComments": false,
  10. "noImplicitAny": false
  11. }
  12. }

systemjs.config.js

</>复制代码

  1. /**
  2. * System configuration for Angular samples
  3. * Adjust as necessary for your application needs.
  4. */
  5. (function (global) {
  6. System.config({
  7. paths: {
  8. // paths serve as alias
  9. "npm:": "node_modules/"
  10. },
  11. // map tells the System loader where to look for things
  12. map: {
  13. // our app is within the app folder
  14. app: "app",
  15. // angular bundles
  16. "@angular/core": "npm:@angular/core/bundles/core.umd.js",
  17. "@angular/common": "npm:@angular/common/bundles/common.umd.js",
  18. "@angular/compiler": "npm:@angular/compiler/bundles/compiler.umd.js",
  19. "@angular/platform-browser": "npm:@angular/platform-browser/bundles/platform-browser.umd.js",
  20. "@angular/platform-browser-dynamic": "npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js",
  21. "@angular/http": "npm:@angular/http/bundles/http.umd.js",
  22. "@angular/router": "npm:@angular/router/bundles/router.umd.js",
  23. "@angular/forms": "npm:@angular/forms/bundles/forms.umd.js",
  24. // other libraries
  25. "rxjs": "npm:rxjs",
  26. "angular-in-memory-web-api": "npm:angular-in-memory-web-api",
  27. },
  28. // packages tells the System loader how to load when no filename and/or no extension
  29. packages: {
  30. app: {
  31. main: "./main.js",
  32. defaultExtension: "js"
  33. },
  34. rxjs: {
  35. defaultExtension: "js"
  36. },
  37. "angular-in-memory-web-api": {
  38. main: "./index.js",
  39. defaultExtension: "js"
  40. }
  41. }
  42. });
  43. })(this);

npm install

touch .bowerrc

</>复制代码

  1. {
  2. "directory": "./client/bower_components"
  3. }

bower install bootstrap --save

Demo

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

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

相关文章

  • mongo + express + ng2 + nodejs tasklist

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

    chanjarster 评论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条评论

187J3X1

|高级讲师

TA的文章

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