'错误:预计422“不可处理的实体”,得到200“OK”'node.js / express app

时间:2017-10-27 01:31:03

标签: javascript node.js express

我经常抛出以下错误

unsigned long

下面是我的完整Error handling invalid query params: Error: expected 422 "Unprocessable Entity", got 200 "OK"

/app.js

我怎样才能更好地处理带有node.js / express的我的无效查询参数,或许可以停止抛出此错误?

我已经安装了express-validation - 并希望这条路线能够解决错误;但我不确定如何从给出的例子中实施。

'use strict'
var path = require('path');
var meal = require('./meals.js');
var express = require('express');
var router = express.Router();
var app = express();

module.exports = function(app) {

  app.get('/allMeals', meal.getAllMeals, END);

  app.get('/meals/:id', meal.getMealinfo, END, function(req, res, next) {
      meal.findById(req.params.id).exec()
        .then(function(xxx) {
          if (meal == null) return next(); // Not found
          return res.send('Found meal '+request.params.id);
        })
        .then(null, function(err) {
          return next(err);
        });
  });

  app.get('/meals/:id/options',meal.getHealthymeals, END);

  function END(req,res){
    res.end();
  }  

  res.status(404).send({
   message: 'Router not found.'
  });

  });
};

0 个答案:

没有答案