Initialize directory:
npm init
Copy the code
Express installation:
npm install express --save
Copy the code
HelloWorld:
To create the app. Js
// import express const express = require()'express'Const app = express() // Build request app.get()'/',(req, res)=>res.send('hello world'App.listen (3000, ()=>console.log()'Example app listening on port 3000! '))
Copy the code
The Express application Generator is a quick skeleton for creating applications
Global Installation Of Express-Generator
npm install express-generator -g
Copy the code
Use the generator to create the project
express --view=pug service-manage
Copy the code
Install dependencies
cd service-manage
npm install
Copy the code
Start the application
DEBUG=service-manage:* NPM start //Windows listens to port 3000 by defaultset DEBUG=service-manage:* & npm start
Copy the code
Basic Route Configuration
1. Route file routes->tools.js
var express = require('express'Var router = express.router (); /* tools listing */ router.get('/'.function (req, res ,next) {
    res.send('Welcome to visit tools... ')
})

router.post('/'.function (req, res) {
    res.send('Got a POST request')
})

router.put('/user'.function (req, res) {
    res.send('Got a PUT request at /user')
})

router.delete('/user'.function (req, res) {
    res.send('Got a DELETE request at /user'}) // Exports tools route module.exports = routerCopy the code
App.js Configures the new route file
// Import toolsRouter module var toolsRouter = require('./routes/tools'); // Add routing rules app.use('/tools', toolsRouter)
Copy the code
The routing configuration
Router-. all() Preprocesses routes with the same routing rules
router.all('/'.function (req, res, next) {
    console.log('Accessing the secret section ... ')
    next() // pass control to the next handler
})
Copy the code
Request parameters
configuration
router.get('/users/:userId/books/:bookId'.function (req, res ,next) {
    res.send(req.params)
})
Copy the code
Test
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34"."bookId": "8989" }
Copy the code

Swagger configuration for background development must do

I. Installation:

npm i express-swagger-generator --save-dev
Copy the code

Two, basic configuration

const express = require('express');
const app = express();
const expressSwagger = require('express-swagger-generator')(app);

let options = {
    swaggerDefinition: {
        info: {
            description: 'This is a sample server',
            title: 'Swagger',
            version: '1.0.0',
        },
        host: 'localhost:3000',
        basePath: '/v1',
        produces: [
            "application/json"."application/xml"
        ],
        schemes: ['http'.'https'],
		securityDefinitions: {
            JWT: {
                type: 'apiKey'.in: 'header',
                name: 'Authorization',
                description: "",
            }
        }
    },
    basedir: __dirname, //app absolute path
    files: ['./routes/**/*.js'] //Path to the API handle folder
};
expressSwagger(options)
app.listen(3000);
Copy the code

Third, view the preview

http://localhost:3000/api-docs/api-docs
Copy the code

Four, swagger. IO

http://editor.swagger.io/#
Copy the code

Background development must do Swagger Configuration 2

Installation:
npm install swagger-jsdoc swagger-ui-express --save
Copy the code
Config.js has been added to the project directory
// exports.swaggerConfig = {openAPI:'3.0.0',
    title: 'Node Service API',
    version: '1.0.0',
    apis: ['./routes/*.js'],
    routerPath: '/api-docs'
}
Copy the code
Item directory added \swagger\index.js\
var swaggerUi = require('swagger-ui-express');
var swaggerJSDoc = require('swagger-jsdoc');
var config = require('.. /config')

exports.setSwagger = function (app) {
    const options = {
        definition: {
            openapi: config.swaggerConfig.openapi,
            info: {
                title: config.swaggerConfig.title,
                version: config.swaggerConfig.version
            }
        },
        apis: config.swaggerConfig.apis
    }

    const swaggerSpec = swaggerJSDoc(options)

    app.get('/api-docs.json', (req, res) => {
        res.setHeader('Content-Type'.'application/json')
        res.send(swaggerSpec)
    })
    app.use(config.swaggerConfig.routerPath, swaggerUi.serve, swaggerUi.setup(swaggerSpec))
}
Copy the code
App. Js configuration
var swaggerConfig = require('./swagger')... var app = express(); . swaggerConfig.setSwagger(app)Copy the code

Use PM2

Installation:
npm install pm2 -g
Copy the code
Activation:
pm2 start app.js
Copy the code
Other:
pm2 list

pm2 stop 0

pm2 restart 0

pm2 show 0

pm2 delete 0
Copy the code