Nodejs writes to the server, asks how to write these interfaces separately in different files, and finally introduces all the interface files into one main file.

I don"t have much time to contact nodejs. I use nodejs to write the server. Now I write all the interfaces in one file. How to write these interfaces separately in different files, and finally introduce all the interface files into a main file

all my interfaces now look like the following code

app.all("/application/message/insert", (req, res) => {
    let insertSql = "INSERT INTO application_message SET ?"; //ID
    let loginUserInfo = {
        "uid": 0
    };

    let param = req.body;
    let selectParams = {
        title: param.title,
        userID: 0,
        content: param.content,
        position: param.position,
        salary: param.salary,
        type: param.type,
        dateline: getCurrentDate(2),
        workPlace: param.workPlace
    };

    if (req.session.loginUserInfo !== undefined) {

        loginUserInfo = req.session.loginUserInfo;

        selectParams["userID"] = loginUserInfo["uid"];
        pool.getConnection( (err, connection) => {
            if (err) {
                throw err;
            } else {
                connection.query(
                    insertSql,
                    selectParams,
                    (err,data) => {
                        if(err) {
                            throw err;
                        } else {

                            res.json({
                                "function": "insert",
                                "message": "success",
                                "detail": ""
                            })
                        }
                    }
                )
            }
            pool.releaseConnection(connection);
        });

    } else {
        res.json({
            "function": "insert",
            "message": "fail",
            "detail": ""
        })
    }
});

/**
 * 
 */
app.all("/application/message/select", (req, res) => {
    let selectSql = "SELECT id, title, content, dateline, userID, a.position, a.salary, a.type, workPlace, u.name as author, u.sex as sex from application_message a, user_information u where a.userID = u.uid and id=? limit 0,1"; //ID

    // let id = req.query.id;

    let id = req.body.id;

    // console.log("uid ==", uid);

    // console.log("");

    let returnData = {
        code: "",
        message: "",
        data: ""
    };
    pool.getConnection( (err, connection) => {
        if (err) {
            throw err;
        } else {
            connection.query(
                selectSql,
                [id],
                function (err, rows) {
                    //console.log(result);
                    //res.json(result);

                    if (err) {
                        throw err;
                    } else {
                        // console.log("loginUserInfo ==", loginUserInfo);
                        returnData["code"] = "0001";
                        returnData["message"] = "success";
                        returnData["data"] = rows;
                        res.json(returnData);
                    }
                    // connection.release();
                }
            )
        }
        pool.releaseConnection(connection);
    })

});

what I'm using:

const express = require('express');
const application = require('./routes/application'); // 
const user = require('./routes/user'); // 
const shortMessage = require('./routes/short_message'); // 

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended:true})); // for parsing application/x-www-form-urlencoded
app.use('/application',application);
app.use('/user',user);
app.use('/shortMessage',shortMessage);

const server = app.listen(3090, function () {
    let host = server.address().address;
    let port = server.address().port;
    console.log('heart web listening at http://%s:%s', host, port);
});

Menu