The main purpose is to use node. js to link MongoDB with Mongoose. With the Ejs engine, extending to. html is easier
The small example has a simple structure and a clear frame.
post
Submission method |
Path |
method |
effect |
get |
add |
|
|
Td>
add |
|
commit record |
get |
del |
|
|
get |
modify |
|
Go to modify page |
post |
modify |
|
modify record |
Portal: Mongodb.js, Data Model: Model.js, (both in the same level directory) route: Mong_ro.js (introduced with Model.js. /)
Three pages index.html,add.html,modify.html
Mongodb.js:
var express = require (' Express '), var path = require (' path '), var favicon = require (' Serve-favicon '); var logger = require (' m Organ '), var cookieparser = require (' Cookie-parser '), var bodyparser = require (' Body-parser '), var routes = require ('./ Routes/mong_ro '); var users = require ('./routes/users '); var app = Express ();//View engine Setupapp.set (' views ', Path.join (__dirname, ' views ')); App.set (' View engine ', ' Ejs '); App.engine (' HTML ', require (' Ejs '). RenderFile);// Uncomment after placing your favicon in/public//app.use (favicon (__dirname + '/public/favicon.ico ')); App.use (' Logger ' Dev '); App.use (Bodyparser.json ()); App.use (bodyparser.urlencoded ({extended:false})); App.use (Cookieparser ()); App.use (Express.static (Path.join (__dirname, ' public ')); App.use ('/', routes); App.use ('/users ', users);//Catch 404 and forward to the error handlerapp.use (function (req, res, next) {var err = new error (' Not Found '); Err.status = 404; Next (err);}); /error handlers//development error handler//would print sTacktraceif (App.get (' env ') = = = ' Development ') {App.use (function (err, req, res, next) {Res.status (Err.status | | 500); Res.render (' error ', {message:err.message, error:err}); });} Production error handler//no stacktraces leaked to Userapp.use (function (err, req, res, next) {Res.status (Err.statu s | | 500); Res.render (' error ', {message:err.message, error: {}}); Module.exports = app;
Model.js:
var express = require (' Express '), routes = require ('./routes '), Mongoose = require (' Mongoose '), Schema = Mongoose. Schema;var Demoschema = new Schema ({uid:string,title:string,content:string,createtime: {type:date, Default:Date.now} }); exports. Demo = Mongoose.model (' blog ', Demoschema);
Mong_ro.js:
var express = require (' Express '), router = Express. Router (), Mongoose = require (' Mongoose '), model = require (' ... /model '), Demo = model. Demo;mongoose.connect (' Mongodb://localhost/blog ');/* GET home page. *///router.get ('/', function (req, res) {//Res.render (' index ', {title: ' Express '})//}); Router.get ('/', function (req , res) {Demo.find (err, Docs) {res.render (' index.html ', {title: ' Express Demo Example ', Demos:docs});}); Router.get ('/add ', function (req, res) {res.render (' add.html ', {title: ' Add demo list '}); Router.post ('/add ', function (req, res) {var demo = new Demo ({UID:REQ.BODY.UID,TITLE:REQ.BODY.TITLE,CONTENT:REQ.BODY.C Ontent});d Emo.save (function (err, doc) {res.redirect ('/');})}); Router.get ('/del ', function (req, res) {var id = req.query.id;if (id && '! = ID) {Demo.findbyidandremove (ID, function (err, docs) {res.redirect ('/');});}}); Router.get ('/modify ', function (req, res) {var id = req.query.id;if (id && '! = ID) {Demo.findbyid (ID, function (err, Docs) {res.render (' modify.html ', {title: ' Modified ', Demo:docs});}); Router.post ('/modify ', function (req, res) {var demo = {Uid:req.body.uid,title:req.body.title,content:req.body.conte Nt};var id = req.body.id; if (id && '! = ID) {demo.findbyidandupdate (ID, demo,function (err, Docs) {res.redirect ('/');});}); /post method always first constructs the method of the Demo object//demo: Find,save,findbyidandremove,findbyid,findbyidandupdate//get request, with Req.query.id;post request , with req.body.id.module.exports = router;
Add.html:
<! DOCTYPE html>
Index.html:
<! DOCTYPE html>Modify.html:
<! DOCTYPE html>
On this basis can continue to expand, the learning of Bootstrap Plus, the use of the jquery UI Plus.
Node.js+express+mongodb