错误[ERR_HTTP_HEADERS_SENT]:NODEJS应用

时间:2019-02-17 09:06:38

标签: node.js http if-statement authentication http-headers

所以在过去的4天里,我一直在从事一个项目,但是每次我尝试返回响应时,即使返回响应,我都会收到已发送的标题错误。这是我的代码如下:

var express = require("express");
var router = express.Router();

// userController import
const userController = require("../controllers/userController");

/* GET home page. */
router.get("/", function(req, res, next) {
  res.render("index", { title: "Express" });
});

router.post("/signup", userController.validate("signup"), (req, res) => {
  if (res.locals.errors != undefined) {
    console.log("res.locals.errors: " + res.locals.errors);
    return res.status(403).json({ errors: res.locals.errors });
  } else {
    res.json({ success: true });
  }
});

module.exports = router;

这是第二个文件:

const User = require("../models/User");

const validateSignupInput = require("../middleware/validation/signup");
const validateLoginInput = require("../middleware/validation/login");

exports.validate = method => (req, res, next) => {
  switch (method) {
    case "signup": {
      const { data, errors, isValid } = validateSignupInput(req.body);
      if (!isValid) {
        res.locals.errors = errors;
        next();
      } else {
        res.locals = { ...data };
        next();
      }
    }
    case "login": {
      const { data, errors, isValid } = validateLoginInput(req.body);
      if (!isValid) {
        res.locals.errors = errors;
        next();
      } else {
        res.locals = { ...data };
        next();
      }
    }
  }
};

我得到的错误:

POST /signup 403 57.554 ms - 49
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/response.js:170:12)
    at done (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/response.js:1004:10)
    at tryHandleCache (/Users/zara/Work/Coding/EEBLAB/node_modules/ejs/lib/ejs.js:257:5)
    at View.exports.renderFile [as engine] (/Users/zara/Work/Coding/EEBLAB/node_modules/ejs/lib/ejs.js:482:10)
    at View.render (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/response.js:1008:7)
    at /Users/zara/Work/Coding/EEBLAB/app.js:39:7
    at Layer.handle_error (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/router/index.js:315:13)
    at /Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/zara/Work/Coding/EEBLAB/node_modules/express/lib/router/index.js:275:10)

如果有人可以帮助我,我将非常高兴。过去四天一直在谷歌上搜索,一直在寻找stackoverflow,我的头好痛...哎呀!

0 个答案:

没有答案