How does node use the express framework to uniformly process return values such as code and msg?

cuiyanlong82 注册会员
2023-02-27 10:41
I don't know if you have solved this problem, if not:

If you have solved the problem, I really hope you can share the solution, write a blog about it, and put a link in the comments section to help more people. ^-^ /div>
dujian0602 注册会员
2023-02-27 10:41

Monster Group and Gpt-based tuning:
The return values can be uniformly handled by writing a custom middleware that can handle all the routing handlers after they have executed.

Here is an example code:

// 引入 express 和自定义的路由处理函数
const express = require('express');
const { getUsers, createUser } = require('./handlers');

const app = express();

// 自定义中间件,用于统一处理返回值
const responseHandler = (req, res, next) => {
    code: res.statusCode,
    msg: res.statusMessage,
    data: res.locals.data

// 注册中间件和路由处理函数
app.get('/users', getUsers, responseHandler);
app.post('/users', createUser, responseHandler);

// 启动服务器
app.listen(3000, () => {
  console.log('Server started on port 3000');

The responseHandler middleware in the above code will process the return value after the route handler has finished executing. This middleware stores HTTP response status codes, status messages, and routing functions in the res.locals object, and returns these values encapsulated in a single object.

In a routing handler function, you can set the portion of the data returned by setting res.locals.data, for example:

const getUsers = (req, res, next) => {
  const users = [ { name: 'Alice' }, { name: 'Bob' } ];
  res.locals.data = users;

When processing the /users route, the above handler returns a JSON object containing an array of users, where the data portion is handled uniformly by the responseHandler middleware.
If the answer is helpful, please take it!

duxingyun217 注册会员
2023-02-27 10:41

This answer references ChatGPT
Middleware can be used in Express to implement the need for uniform handling of code, msg, etc return values. You can write a middleware function that is called on each route response. Here is sample code that implements this middleware:

// 统一返回值中间件
const resultMiddleware = (req, res, next) => {
  // 重写 res.json 方法
  res.json = (data, code = 0, msg = 'success') => {
      code: code,
      msg: msg,
      data: data

// 在 app.js 中引入该中间件

In this middleware function, we overrode the res.json method to reorganize the incoming parameters into a uniform format for return. In the response of each route, you just need to call the res.json function with the parameters data, code, and msg.

For example, if we wanted to return a piece of data {name: 'Tom', age: 18}, we would write:

app.get('/api/user', (req, res) => {
  const data = { name: 'Tom', age: 18 }

The format of the returned data is uniform, as follows:

  "code": 0,
  "msg": "success",
  "data": {
    "name": "Tom",
    "age": 18

This way you can normalize the format of the interface return values and reduce code redundancy.

About the Author

Question Info

Publish Time
2023-02-27 10:41
Update Time
2023-02-27 10:41