1. Express simply builds a local service of 3 components server.js

Install the Express and Nodemon hot updatesCopy the code
var express = require("express"); Var app = express(); // Create a web server app.listen(9527, function(){console.log("server listening on 9527"); })Copy the code

2. Connect the MongoDB database

1. Register and log in to the mongoDB cloud storagecloud.mongodb.com/Choose the free one and follow the steps below

2. Install mongoose locally in server.js reference
var express = require("express"); // import express const mongoose=require('mongoose'); var app = express(); // Create a Web server mongoose.connect("mongodb+srv://chenhui:[email protected]/myFirstDatabase?retryWrites=true&w=majo Rity ",{useNewUrlParser: true}). Then (()=>{console.log(' Mongoose successfully connected! ')}). Catch (err=>{console.log(err)}) app.listen(9527, function(){console.log("server listening on 9527"); })Copy the code

At this point, the connection is successful

{useNewUrlParser: true}useNewUrlParser is set to true to avoid the “current URL string parser is not recommended” warning

3. Create a new models folder –> create user.js

const mongoose=require('mongoose'); const Schema=mongoose.Schema; Const userSchema = new Schema({name: {type: String, // type required:true,}, email:{type: String, // Type required:true,}, password:{type:String, required:true}, date:{type:String, default:new date ()}, avatar: { type: String, }, }); // const db= { // User: mongoose.model('MUser', userSchema), // }; // module.exports = db; module.exports = User=mongoose.model('users', userSchema); #### server.jsCopy the code
var express = require("express"); // import express const mongoose=require('mongoose'); const users=require('./routes/api/user'); const bodyParser=require('body-parser') var app = express(); // Create a Web server mongoose.connect("mongodb+srv://chenhui:[email protected]/myFirstDatabase?retryWrites=true&w=majo Rity ",{useNewUrlParser: true}). Then (()=>{console.log(' Mongoose successfully connected! ') }) .catch(err=>{ console.log(err) }) app.use('/api/user',users); Listen (9527, function(){console.log("server listening on 9527"); })Copy the code

4. Build the routing module “routes” –> “user.js

const express=require('express'); const router=express.Router(); const User=require('.. /.. /models/User'); Router.get ('/allList',(req,res)=>{res.json({MSG :' hello '})})Copy the code

Note: We need to install body-Parser for post requests, otherwise the data returned will not be available in server.js

const bodyParser=require('body-parser') app.use(bodyParser.urlencoded({extended:false})); App.use (bodyParser.json())// Return as jsonCopy the code

5. Register the interface and operate the Mongoose Api operation documentMongoosejs.com/docs/api/mo…

// Post requests must have body-parser/API /user/register router.post('/register',(req,res)=>{// console.log(req.body) FindOne ({email:req.body.email}). Then ((User)=>{if(User){// Check whether the email has been registered with return Res.status (400).json({email:' Email has been registered! '}) }else{ const newUser=new User({ name:req.body.name, email:req.body.email, Password: the req. Body. Password}) / / password is encrypted and saved to the cloud database bcrypt. GenSalt (10, function (err, Hash (newuser. password, salt, (err, hash)=> {// if(err) {throw new Error(err)}; newUser.password=hash; Then (user=>res.json(user)). Catch (err=>console.log(err))}); }); }})})Copy the code

Note: bcrypt encryption use methodwww.npmjs.com/package/bcr…The test results are as follows:–> Here you can see the five inserts

Query all registered data
Router.get ('/allList',(req,res)=>{//find(); user.find (). Then (all=>{ res.status(200).json({success:true,data:all,length:all.length}) }) })Copy the code