请求接口 : http://localhost:8000/api/v1/userss 接口不存在则 返回自定义404错误处理


定义404中间件

  1. const HttpException = require('../exception/http.exception')
  2. const noMatchMiddleware = (req,res,next)=>{
  3. res
  4. .status(404)
  5. .json({
  6. code:0,
  7. message:'Router Url not found'
  8. })
  9. }
  10. module.exports = noMatchMiddleware

使用404中间件

  1. app.use(noMatchMiddleware)