Express experience routing, template engine, middleware

http://expressjs.com/en/4x/api.html#req.method

http://expressjs.com/en/guide/routing.html

 

【Route paths】Route parameter = path parameter corresponding to console.dir(req.params)

This supports dynamic URLs, so a request for a resource can be /resource/:id or even the regular /resource/1?

 

[Query string] Query string = queryParams corresponds to console.dir(req.query)

 

[req.body request body]

[Use the Postman tool to mock the Post request] Saves the time to create a form

[Use body-parser middleware to process post requests (before handler)] https://github.com/expressjs/body-parser

 

[Using multer middleware to upload files] https://www.npmjs.com/package/multer

 

[Use template engine ejs] ejs.co

1. Quite like the jsp expression <%= %>, simply use the path parameters (req.params) to complete the client-server communication

2. app.set('view engine','ejs')

 

[Use middleware processing] app.use(path,cb)

Assuming that there is mid1->mid2, that is, mid1 calls next and passes it to mid2. When mid2 completes the control transfer and returns to mid1, there is mid2->mid1.

To sum up, there is a process of mid1<=>mid2, which is passed to the past and passed back.

[Example, Express built-in middleware handles static resources] app.use('/public', express.static('public'))

 

[Middleware realizes the separation of routing] server > router + handler i.e. app.use('/',indexRouter)

 

……………………………………………………………………………………………………………………………………

Todolist Small Project

Initialize npm init > npm install --save express ejs body-parser 

【MVC】Controller = router + handler

[Write a page] You need jq+css, which is generally a static resource in the public folder. Take it and use it, and use the GET method to display a form in the Controller (provides the ability to add an entry later).

【Realize function】

Added: Parse the request body req.body with body-parser

Delete: use data.filter(condition) //condition is true to leave

Check: Client forEach

[Persistence] Use mongoose https://github.com/Automattic/mongoose

 

 

 

 

 

 

app.use('/assets', express.static('public'));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771648&siteId=291194637
Recommended