preface
It has been more than two months since I updated my blog last time. In the past two months, I have been busy with the company’s projects during working hours, and I have also done some outsourcing projects with small programs and management projects after work. It is also possible that the outsourcing projects I was working on during that period were also in a hurry, so I had to sleep around a little at night, severely squeezing my rest time, and it took me a long time to recover. Of course, I also found the advantages and disadvantages of busy outsourcing, the advantages can be more project experience, for example, the small program is audio and video education applications, but also to understand some company projects do not need to use the API; The management side also applied Vue + Element-UI development, which laid some foundation for the company’s project. Bad, is really tired, also because few know the market, the price is not high, still low, so to speak, after the asked friends said my price low, heart tired ah ~ ~ ~, and do the project actually say deep, on its own technology to improve the effect of feeling, so also summarizes technology enhance or need precipitation, not can’t blindly to do more projects. Node.js has not been updated for a long time, and now it continues to be updated, there are not bits or errors in the place hope that the big guy pointed out.
Express
Express (Part 1) mainly describes concepts such as Express framework structure, routing, and middleware. In this chapter, we mainly implement simple functions such as user add, change and search (temporarily without using database), and get familiar with the development of Express framework. Again, install the Express-Generator application skeleton and use ejS templates
//1. Install express-generator NPM install expess-generator-g //2. Express --view= EJS //3 The installation depends on NPM installCopy the code
Frame structure: Create a new Models folder on top of the original frame to hold the user model
. ├ ─ ─ app. Js ├ ─ ─ bin │ └ ─ ─ WWW ├ ─ ─ package. The json ├ ─ ─ models │ └ ─ ─ in_memo │ └ ─ ─ the users. The js ├ ─ ─ public │ ├ ─ ─ images │ ├ ─ ─ ├─ ├─ ├─ javascripts │ ├─ ├─ ├─ javascripts │ ├─ ├─ ├─ javascripts │ ├─ ├─ javascripts │ ├─ ├─ The index. The ejs └ ─ ─ layout. EjsCopy the code
In this way, we have built the general framework.
User add change check
1. Create a user model: construct a user class, create a user, obtain a user list, obtain a user, update the user
/** * Created by aaron. * description:User Model */ let USER_ID_INT=10000; let users=[]; class User{ constructor(params){ if(! params.name || ! params.age) throw new Error('name and age is required when create a new user.') this.name=params.name this.age=params.age this._id=USER_ID_INT++ } } async function createANewUser(params) { const user=new User(params) users.push(user) return user } async function getUsers() { return users } async function getUserById(userId,params) { const user=users.find(u => u._id===userId) console.log(user) return user } async function updateUserById(userId,params) { const user=users.find(u=>u._id===userId) if(params.name)user.name=params.name if(params.age)user.age=params.age return user } module.exports={ model:users, createANewUser, getUsers, getUserById, updateUserById }Copy the code
2. Create the users.js route on routes to add and change users
const express = require('express'); const router = express.Router(); const Users=require('.. /models/in_memo/users') /* GET users listing. */ router.route('/') .get((req, res, next) => { (async()=>{ const users=await Users.getUsers() return { code:0, users:users } })() .then((r)=>{ res.json(r) }) .catch((e)=>{ next(e) }) }) .post((req,res,next)=>{ (async()=>{ const user=await Users.createANewUser({ name:req.body.name, age:req.body.age }) return { code:0, user:user } })() .then((r)=>{ res.json(r) }) .catch((e)=>{ next(e) }) }); router.route('/:id') .get((req,res,next)=>{ (async()=>{ const user=await Users.getUserById(Number(req.params.id)) return { code:0, user:user } })() .then((r)=>{ res.json(r) }) .catch((e)=>{ next(e) }) }) .patch((req,res,next)=>{ (async ()=>{ const user=await Users.updateUserById(Number(req.params.id),{ name:req.body.name, age:req.body.age }) return { code:0, user:user } })() .then(r=>{ res.json(r) }) .catch(e=>{ next(e) }) }) module.exports = router;Copy the code
3. Add a route and mount it to app.js
After starting the project, we can test the project function through Postman.
Create a new user screenshot:
Get user screenshots:
conclusion
In this way, we can complete the function of adding and updating users through the Express framework. The complete code will be uploaded to GitHub later, and the application of Mongoose database will also be added. Also hope to have big guy to point out missing or wrong, thank you.
Author: PC. Aaron
Reference: www.cnblogs.com/aaron-pan/
The copyright of this article belongs to the author, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in the obvious position of the article page, otherwise reserve the right to pursue legal responsibility.