Basic understanding of express framework

Time:2021-4-19
introduction

*Because nodejs was learned a long time ago, and now we have forgotten the grammar rules. Now we start from the framework and understand nodejs in the opposite way.
It’s very troublesome to create a web server in nodejs. To use the HTTP module, the express framework is a tool to optimize such a process.
When learning express framework, we found that it is similar to Vue in routing. Maybe that’s why Vue and express are both called frameworks, but express is the framework of the server. *

install

Use in terminal
    `cnpm/npm install express`
that will do

use

//You need to bind to an object to use it, so
    const express = require('express');
    //After binding, you need to use this object to create a web server
    const app = express();
    //To add a route, get can only add a specific route. If it is' / ', 404 will be used, but use can be used. See the next article for details
    app.get('/home', (req, res) => {
        res.send (first page)
    })
    //After creation, it needs to be mapped to a port to play a role
    app.listen(3000)
    //Show that the program is running
    console.log ('end of operation ');

Basic functions

If you only use the basic creation server, it's really a waste of express, and it also includes many complex functions

middleware

//Add next to the callback function and specify the target on the last line
        app.get('/home', (req, res, next) => {
            console.log ('jump to next route ');
            next();
        })
        //The next routing address remains the same
        app.get('/home',(req,res)=>{
            res.send ('hop route complete ')
        })
//Or use use to accept all information. If the route is empty, all information will be received
        app.use((req,res)=>{
            res.send ('hop route complete ')
        })

Interception of Middleware

//If the website is being maintained, all requests can be blocked
        app.use((req,res)=>{
            res.send ('site under maintenance ')
        })
        
        //If there is an error, intercept it
        app.get('/index', (req, res) => {
            Throw new error ('Program error ');
        })
        //Use use to receive all error messages. You must fill in next here
        app.use((err, req, res, next) => {
            //Because of the framework used, the status code will be 200 even if there is an error. Here, it is set to 500
            res.status(500).send(err.message);
        })

Intercept asynchronous errors

//Introduction of asynchronous module
        const promisify = require('util').promisify
        //Introduction of read-write module
        const fs = require('fs')
        //Asynchronous read / write module
        const readFile = promisify(fs.readFile)
app.get('/index', async(req, res,next) => {
            try {
                //Randomly set the path to make the program throw an error
                Await readfile ('does not exist. TXT ')
            } catch (err) {
                next(err)
            }
        })
        app.use((err, req, res, next) => {
            res.send(err)
        })

Routing modularization

In express, secondary route can be generated by built-in constructor router
//Create routing object
        const home=express.Router();
        //Use use to point to sub route
        app.use('/home',home)
        //The sub route address is / index
        home.get('/index',(req,res)=>{
            res.send ('sub routing module ')
        })
In the actual development process, the two routes are usually written in different files
    In the main route inside the introduction and path matching

index.js:

const home=require('./home.js')
        app.use('/home',home)

home.js:

const express = require('express');
        const home = express.Router();
        
        home.get('/index', (req, res) => {
            res.send (welcome to the home page)
        });
     module.exports = home