Basic understanding of express framework


*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. *


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


//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
    //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


//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 ');
        //The next routing address remains the same
            res.send ('hop route complete ')
//Or use use to accept all information. If the route is empty, all information will be received
            res.send ('hop route complete ')

Interception of Middleware

//If the website is being maintained, all requests can be blocked
            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

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) {
        app.use((err, req, res, next) => {

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
        //The sub route address is / index
            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


const home=require('./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