Summary:
Express Development Web Interface
Non-relational database MongoDB
Linking and manipulating MongoDB using Nodejs and Mongoose modules
First, Express
Express, Fast, developed, minimalist web development framework based on NODEJS
Install Express:
NPM Install Express--save
The Server.js file is as follows:
Const Express=require (' Express ');//new AppConst app=Express ();//Client Access/When returning a piece of textApp.get ('/',function(req,res) {res.send (' <p>hello world</p> ')})//return JSON data when client accesses/dataApp.get ('/data ',function(req,res) {Res.json ({"Name": "Lizhao", "Age": "2"})})//Monitor 9093App.listen (9093,function() {Console.log (' node app start 9093 ')})
App.get, app.post respectively develop Get,post interface, App.use use module, Res.send, Res.json, res.sendfile respond to different content.
Installing Nodemon
Listens for routing and response content so that the service starts automatically after each modification.
NPM Install Nodemon-g
Start
Nodemon Server.js
Second, non-relational database MongoDB
The installation and use of MongoDB is described in:
Iii. linking and manipulating MongoDB using Nodejs and Mongoose modules
Installing Mongoose
NPM Install Mongoose--save
The JSON is stored by Mongoose Operation Mongodb,mongodb, which is much easier than MySQL.
Basic use of Mongoose
Connect Connection Database
Model new models, schema definition document models
Create,remove,update to delete and change
Find and FindOne to query the data
Const Express=require (' Express '); Const Mongoose=require (' Mongoose ');//connect MONGO, and use IMOOC this collectionConst db_url= ' MONGODB://127.0.0.1:27017/IMOOC '; Mongoose.connect (Db_url); Mongoose.connection.on (' Connected ',function() {Console.log (' Connect ')})//similar to the MySQL table, MONGO has the concept of a document, a field, a new document modelConst User=mongoose.model (' User ',NewMongoose. Schema ({user:{type:string,require:true}, Age:{type:number,require:true}}))//Increaseuser.create ({User:' Lizhao ', Age:19 },function(err,doc) {if(!err) {Console.log (DOC)}Else{Console.log (Err)}})//new AppConst app=Express ();//Client Access/When returning a piece of textApp.get ('/',function(req,res) {res.send (' <p>hello world</p> ')})//JSON data returned when the client accesses/data with the query userApp.get ('/data ',function(req,res) {//CheckUser.find ({},function(err,doc) {if(!err) { returnRes.json (DOC)});})//DeleteUser.remove ({age:18},function(Err,doc) {Console.log (DOC)})//ChangeUser.update ({age:19},{' $set ': {age:20}},function(Err,doc) {Console.log (DOC)})//Monitor 9093App.listen (9093,function() {Console.log (' node app start 9093 ')})
Express+mongodb Developing Web Backend interface