node --- 使用express.Router与body-parser
生活随笔
收集整理的這篇文章主要介紹了
node --- 使用express.Router与body-parser
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
express框架提供了一個Router方法,用于監聽路由
// 命令行(windows*64) npm install express --save // router.js const express = require("express"); // 定義路由 const router = express.Router();// 處理http://host:port/students/ 路由(GET方法) router.get("/students/", function(req, res) {if(err) {return res.status(500).send("Server error.");} res.send("Hi "); });// 處理http://host:port/students/ 路由(POST方法) router.post("/students/", function(req, res) {if(err) {return res.status(500).send("Server error.");}res.send("handle Post"); }); // 注:node原生時 res.end()結束, 單身express封裝了,使用其封裝的方法(send),會自動添加res.end() // app.js const express=require("express"); const app = express(); const router = require("./router.js");// 掛載router app.use(router);body-parser: 用于解析POST請求提交的數據(https://github.com/expressjs/body-parser)
像mongoDB插入一條信息.
mongoDB參考 https://blog.csdn.net/piano9425/article/details/98473425
其中用到了模板引擎參考 https://blog.csdn.net/piano9425/article/details/98477516
1.安裝其依賴: npm install --save body-parser
2.確保mongo數據庫處于打開狀態: 命令行輸入 mongod
node app.js啟動
進入mongo查看所有數據庫
db查看當前正在使用的數據庫(注:因為test無數據,故show dbs中無顯示)
db.test.find() 查看數據(發現為空)
打開瀏覽器(輸入url) -> 提交
打開mongoDB查看(mongo -> use marron -> db.tests.find() )
總結
以上是生活随笔為你收集整理的node --- 使用express.Router与body-parser的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ORACLE使用copy方式存储迁移,详
- 下一篇: 21世纪大学生之你是属于哪种看网课。