1. Set up the basic framework of the project

1.1. Project initialization

npm init -y
Copy the code

1.2. Install the Express framework production dependency

npm i -S express
Copy the code

1.3. Create app.js file in the root directory as the entry file and write it into the entry service program

Const express = require("express"); // Create a service object const app = express(); // Listen for get requests'/'Routing app. Get ("/".function (req, res) {
  res.send("hahah"); }); Const server = app.listen(5000,functionConst {address, port} = server.address(); const {address, port} = server.address(); const {address, port} = server.address(); console.log("Service started please visit http://%s:%s", address, port);
});

Copy the code

1.4. Middleware functions (i.e. callback functions that contain three parameters and are injected at the time of callback) must be placed before the app listens for the request

functionmylogger(req,res,next){ next(); } app.use(mylogger);Copy the code

1.5, routing,

Rules for how applications respond to requests are divided into two parts: Request methods: get and post…… *fly$/……

app.get('/'.function(req, res) {
  res.send('hello node')
})

app.post('/'.function(req, res) {
  res.send('hello node')})Copy the code

1.6. Exception handling

Handle exceptions generated in requests through custom exception handling middleware

app.get('/'.function(req, res) {
  throw new Error('something has error... ')
})

const errorHandler = function (err, req, res, next) {
  console.log('errorHandler... ')
  res.status(500)
  res.send('down... ')
}

app.use(errorHandler)
Copy the code

Note two points when using:

First, one parameter should not be too few, otherwise it will be regarded as ordinary middleware. Second, exception handling middleware needs to be referenced after the request

2. Optimized the project framework

2.1. Custom routing middleware

  • (1) Create the router folder in the root directory and create the index.js file
  • (2) Import the custom routing middleware in app.js file, and then use the middleware through app.use()
Router /index.js /* Custom routing middleware */ /"express"); Const boom = require(const boom = require("boom"); Const userRouter = require(const userRouter = require("./map/user"); Const router = express.router (); const router = express.router (); Router.get ("/", (req, res) => {
  res.send("Zhongyan Online"); }); UserRouter Middleware router.use("/user", userRouter); Router. use((req, res, next) => {next(boom.notfound ())"Interface does not exist")); }); / / define a error processing middleware to the client respond to json format error data router. Use ((err, the req, res, next) = > {/ / wrong information const MSG = (err && err. Message) | |"System error"; / / get error status code const statusCode = (err) output & & err. Output. StatusCode) | | 500; / / get errorMsg const errorMsg = (err. The output && err. The output. The payload && err. The output. The content, the error) | | err. Message; Res.status (statusCode). Json ({code: -1, MSG, error: statusCode, errorMsg,}); }); module.exports = router;Copy the code
Const express = require(app.js)"express"); Const router = require(const router = require("./router"); // Create a service object const app = express(); // Use the routing middleware app.use("/", router); Const server = app.listen(5000,functionConst {address, port} = server.address(); const {address, port} = server.address(); const {address, port} = server.address(); console.log("Service started please visit http://%s:%s", address, port);
});
Copy the code