Express
install
be based on Node.js Platform, a fast, open and simple Web development framework
Basic use
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => res.send('Hello World!')) app.listen(3000, () => console.log(`Example app listening on port 3000!`))
Basic routing
Router
- Request method
- Request path
- Request processing function
get:
// When the / is requested by the GET method, the corresponding processing function is executed app.get('/', (req, res) => res.send('Hello World! GET'))
post:
app.post('/', (req, res) => res.send('Hello World! POST'))
GET the form GET data request body in Express
Express has a built-in API that you can use directly req.query To get
app.post('/sub',function(req, res){ var comment = req.query comment.dateTime = time.format(new Date(), 'YY-MM-DD') comments.push(comment) // redirect res.redirect('/') })
Get the form post data request body in Express
There is no built-in API to get the body of the form Post request in express. A third-party package, body parser, needs to be used here
body-parser
install
npm install body-parser --save
to configure
var express = require('express') var bodyParser = require('body-parser') var app = express() // Configure body parser // As long as this configuration is added, there will be an additional attribute on the req request object: body // Then you can directly pass req.body To get the data of the form POST request app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json())
use
app.post('/post',function(req, res){ var comment = req.body comment.dateTime = time.format(new Date(), 'YY-MM-DD') comments.push(comment) // redirect res.redirect('/') })
Static service
When we directly access the files in / public, we cannot directly access them, so we need to express.static To release files and provide static resource services
//1. The path must be a file under / public/xxx before access under the public directory app.use('/public/', express.static('./public/')) //2. Release the file under public, and access directly without adding public / xxx app.use(express.static('./public/')) //3. If you use / pub / to alias / public /, you need to access the file under public through / pub/xxx app.use('/pub/',express.static('public')) app.use('/pub/aa/',express.static('public ')) / / aliases can also be created in this way
Configuring the use of art template template engine in Express
install
npm install --save art-template npm install --save express-art-template
to configure
- Configure to use art template template engine
- The first parameter indicates that the art template template engine is used when the file ends with. Art
- When you need to render an html file, you can change the art to html
// Although there is no need to record art template outside, it must also be installed // The reason is that express art template relies on art template app.engine('art', require('express-art-template'));
use
- Express provides a method for the corresponding object of Response: render
- The first parameter in render cannot write the path. By default, it will go to the views directory in the project to find the template file. That is to say, Express has a convention: developers put all view files in the views directory
- If you want to modify the default views directory, you can app.set ('views', default path to render function)
app.get('/', function(req, res){ // express defaults to the views directory index.html res.render('index.html', { title: 'This is a title' }) // res.render('404.html') // res.render('admin/404.html ') / / access 404.html under admin under views })
If you want to modify the default views rendering store directory, you can:
// Modify views, modify the default path of render function // app.set ('views', default path to render function) app.set('views', '/show')
redirect
The redirect method allows the redirection of the web address to jump to the specified url and specify the status. The default is 302 mode.
Format: res.redirect([status], url);
//Jump to the specified URL res.redirect("https://mp.csdn.net/"); //Jump to home res.redirect("/");