App. All () does not work. Record it to prevent future problems 😅

Resolve cross-domain (CORS)

Add the following code to app.js to solve the cross-domain problem:

app.all(The '*'.function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin'.The '*')
  res.setHeader('Access-Control-Allow-Methods'.'GET,PUT,POST,DELETE,OPTIONS')
  res.setHeader('Access-Control-Allow-Headers'.'Content-type,Accept,X-Access-Token,X-Key')
  if (req.method == 'OPTIONS') {
    res.status(200).end()
  } else {
    next()
  }
})
Copy the code

However, I encountered a problem. The three fields were not set in the header of the returned message, and only the header fields were set before the returned data. Later, I found that the problem was where this code was added. I put it after app.use() routing, so I set the header only after the response message has been sent, which of course doesn’t work.

Because Express executes the middleware in a defined order, it simply puts app.all() in front of routing processing

Resources: stackoverflow.com/questions/2…