摘要:準備目錄開始后端創建項目前端
準備
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文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/91043.html
摘要:準備目錄開始后端創建項目前端 準備 nodejs mongo 目錄 |-client |-app |-components |-task |-tasks.component.html |-tasks.components.ts |-services...
摘要:在使用過程中我們可以通過增加哈希次數來提高數據的安全性。當然,對密碼的哈希操作應該在保存數據之前。 showImg(https://segmentfault.com/img/remote/1460000010821081); 毫無疑問,幾乎所有的應用都會涉及到數據存儲。但是 Express 框架本身只能通過程序變量來保存數據,它并不提供數據持久化功能。而僅僅通過內存來保存數據是無法應對...
摘要:使用,可參考執行退出命令,只要設置,即可。下節主要實現注冊時的郵件驗證保存登錄狀態異常處理,同步異步操作全棧工程技術新群上一篇公司項目實踐下一篇待續 一、前言 ???書接上回,我們搭建了WEB服務端路由、模板等功能,完成了register 通過ajax與后端的通信,今天主要完成數據與mongodb的存取,實現注冊 / 登錄 / 退出功能 ???DEMO GIT https://gi...
摘要:連接數據庫如果不自己創建默認數據庫會自動生成地址跟第一步的地址對應?,F在回過頭來看里面的入口文件最后,我們在瀏覽器輸入,就會跳到。到此為止,我們就完成了整個前后端各自開發到正式部署的流程。 一個完整的網站服務架構包括:1、web frame ---這里應用express框架2、web server ---這里應用nodejs3、Database ---這里應用monggoDB4、...
閱讀 1743·2021-09-22 15:25
閱讀 1307·2019-08-29 12:34
閱讀 1908·2019-08-26 13:57
閱讀 3188·2019-08-26 10:48
閱讀 1443·2019-08-26 10:45
閱讀 793·2019-08-23 18:23
閱讀 733·2019-08-23 18:01
閱讀 1945·2019-08-23 16:07