Express is a framework for Node that allows you to quickly develop services that handle client requests
The installation
npm i express --save
Copy the code
Import express
let express = require('express');
let bodyParser = require('body-parser');
Copy the code
Create a server using Express
let app = express();
Copy the code
### Use of middleware
app.use(express.static(__dirname)); Use (bodyParser.json()); // Parse the data from the POST request and mount it to the body of the REqCopy the code
The routing configuration
A get request
app.get('/login'.function (req, res) {
console.log(req.query);
res.send(req.headers);
});
Copy the code
A post request
app.post('/register'.function (req, res) {
console.log(req.body);
res.send({
code: 0,
data: {},
msg: 'ok'})});Copy the code
Both get and POST requests are executed
app.all('/api/xyz'.function (req, res) {
console.log('Here comes the request');
});
Copy the code
Listening port number
app.listen(8000, () => console.log('Started successfully'));
Copy the code