node.js/express 应用程序中的错误:发送后无法设置标头

卡米尔·塞巴斯蒂安·尼森

我在设置 Node.js 应用程序时遇到问题,使用 Express 进行路由,使用 MongoDB (Mongolab) 设置数据库。我一直在环顾四周,发现很多其他用户也有同样的问题,但还没有找到解决问题的方法。希望有人能提供帮助。

我不断收到的错误是 Error: Can't set headers after they are sent.

POST /login请求后我收到错误我试过使用res.end();,res.redirect('/users/index');并返回渲染..但还没有成功..

这里最重要的代码:

服务器.js

  /* LOAD ALL DEPENDENCIES
    ----------------------------------------- */
    const express = require('express');
    const path = require('path');
    const request = require('request');
    const session = require('express-session');
    const compression = require('compression');
    const bodyParser = require('body-parser');
    const app = express();

    /* MONGODB CONFIGURATION
    ----------------------------------------- */
    const MongoClient = require("mongodb").MongoClient;
    require('dotenv').config();
    const dbConfig = process.env.MONGODB_URI;

    MongoClient.connect(dbConfig, (err, database) => {
      if (err) return console.log(err)
      db = database
    });

    /* SESSIONS CONFIGURATION
    ----------------------------------------- */
    app.use(session({
        secret: "JA1d82JHYF9?nsdfDF635MuHe#ksd",
        resave: false,
        saveUninitialized: true
    }));

    /* SET PORT FOR HEROKU
    ----------------------------------------- */
    const port = process.env.PORT || 3000;
    const host = process.env.HOST ||'0.0.0.0';

    // Rest of the code ..

    app.use('/users', usersRouter);
    // Router is loaded earlier

    /* START THE NPM SERVER
    ----------------------------------------- */
    app.listen(port, host, function() {
        console.log(`Server started on port ${port}`);
    });

users.js(路由)

/* LOAD ALL DEPENDENCIES
----------------------------------------- */
const express = require('express');
const router = express.Router();
const passwordHash = require('password-hash');

/* INDEX ROUTE
----------------------------------------- */
router.get('/', function(req, res) {
  if (req.session.login) {
    res.render('users/index');
  } else {
    res.redirect('/users/login');
  }
});

router.get('/login', function(req, res) {
  res.render('users/login');
});

router.post('/login', function(req, res) {
  const loginName = req.body.username;
  const loginPassword = req.body.password;
  db.collection('users').find().toArray(function(err, results) {
    results.map(function(user) {
      if (user['username'] === loginName) {
        const pwCheck = passwordHash.verify(loginPassword, user['password']);
        if(pwCheck === true) {
          req.session.login = true;
          req.session.username = user['username'];
          res.render('users/index');
        } else {
          res.redirect('/users/')
        }
      }
    })
  });
});

完全错误

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/response.js:725:10)
    at ServerResponse.send (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/response.js:170:12)
    at done (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/response.js:962:10)
    at tryHandleCache (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/ejs/lib/ejs.js:208:10)
    at View.exports.renderFile [as engine] (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/ejs/lib/ejs.js:412:10)
    at View.render (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/view.js:128:8)
    at tryRender (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/application.js:640:10)
    at EventEmitter.render (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/camillesebastien/Documents/Programming/Mongo App/node_modules/express/lib/response.js:966:7)

我的问题

这个错误究竟是什么意思,出了什么问题?在这种情况下我该如何解决这个问题?

我正在使用 Express 作为路由器和 mongoDB 作为数据库在 Node.JS 中构建。会不会和那个有关系?

我在 Stackoverflow 上检查了很多类似的问题,但还不能解决我的问题..

希望有人可以帮助我,在此先感谢!

突然冒出一个想法:

问题可能是我在本地主机上测试,而 mongodb (mongolab) 设置在 Heroku 上?

彼得布

您的问题是,对于您的数据库查找返回的每个结果值与您正在调用的密码和用户名匹配res.render()res.render()每个请求只能调用一次。所以理论上,如果只返回 1 个用户并且他们的密码匹配,这将起作用,但是如果同一个用户多次存在并且具有相同的密码,您将res.render()在它已经发送一次后拨打电话

我的建议是遍历返回的用户列表,然后在第一次匹配后退出。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

node.js应用程序:抛出新错误(“发送头后无法设置头。”)

来自分类Dev

错误:发送标头后无法设置标头-Node.js

来自分类Dev

Node.js流(错误:发送标头后无法设置标头)

来自分类Dev

Node.js出现错误:发送标头后无法设置标头

来自分类Dev

引入 res.render() 后,Node.js 发送后无法设置标头错误

来自分类Dev

Express / node.js发送后无法设置标头

来自分类Dev

Node、Mongo、Mongoos - 发送后无法设置标头

来自分类Dev

node-rest-client:发送后无法设置标头。

来自分类Dev

Node.js/Firebase:发送后无法设置标头

来自分类Dev

Node.JS / Passport-错误:发送标头后无法设置标头。在ServerResponse.OutgoingMessage.setHeader

来自分类Dev

Node.js错误:发送标头后无法设置标头,有时会出现

来自分类Dev

Node.js:http.js:691抛出新错误(“发送后无法设置标头。”)

来自分类Dev

Node.js“发送后无法设置标头”错误消息

来自分类Dev

Node.js UnhandledPromiseRejectionWarning:错误:发送后无法设置标头

来自分类Dev

node.js错误-发送标头后无法发送标头

来自分类Dev

node.js错误-发送标头后无法发送标头

来自分类Dev

在node.js中发送标头后无法设置标头

来自分类Dev

Node.js:发送标头后无法设置标头

来自分类Dev

带有节点的Express应用程序中的“错误:发送标头后无法设置标头”

来自分类Dev

将标头发送到node.js中的客户端后无法设置标头

来自分类Dev

在node.js / mongoDB中更新时,无法在发送标头后设置标头

来自分类Dev

错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头-Node Express Mongodb

来自分类Dev

带有node.js的http请求失败,发送后无法设置标头

来自分类Dev

Node / Express 添加<% include %> 时发送后无法设置标头

来自分类Dev

发送到客户端后无法设置标头 - Node/Mongoose/Express

来自分类Dev

Node.js 344错误发送头后无法设置头

来自分类Dev

Node.js 344错误发送头后无法设置头

来自分类Dev

使用 mongoose 和 node.js 将标头发送到客户端后无法设置标头

来自分类Dev

无法在node.js中设置标头

Related 相关文章

  1. 1

    node.js应用程序:抛出新错误(“发送头后无法设置头。”)

  2. 2

    错误:发送标头后无法设置标头-Node.js

  3. 3

    Node.js流(错误:发送标头后无法设置标头)

  4. 4

    Node.js出现错误:发送标头后无法设置标头

  5. 5

    引入 res.render() 后,Node.js 发送后无法设置标头错误

  6. 6

    Express / node.js发送后无法设置标头

  7. 7

    Node、Mongo、Mongoos - 发送后无法设置标头

  8. 8

    node-rest-client:发送后无法设置标头。

  9. 9

    Node.js/Firebase:发送后无法设置标头

  10. 10

    Node.JS / Passport-错误:发送标头后无法设置标头。在ServerResponse.OutgoingMessage.setHeader

  11. 11

    Node.js错误:发送标头后无法设置标头,有时会出现

  12. 12

    Node.js:http.js:691抛出新错误(“发送后无法设置标头。”)

  13. 13

    Node.js“发送后无法设置标头”错误消息

  14. 14

    Node.js UnhandledPromiseRejectionWarning:错误:发送后无法设置标头

  15. 15

    node.js错误-发送标头后无法发送标头

  16. 16

    node.js错误-发送标头后无法发送标头

  17. 17

    在node.js中发送标头后无法设置标头

  18. 18

    Node.js:发送标头后无法设置标头

  19. 19

    带有节点的Express应用程序中的“错误:发送标头后无法设置标头”

  20. 20

    将标头发送到node.js中的客户端后无法设置标头

  21. 21

    在node.js / mongoDB中更新时,无法在发送标头后设置标头

  22. 22

    错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头-Node Express Mongodb

  23. 23

    带有node.js的http请求失败,发送后无法设置标头

  24. 24

    Node / Express 添加<% include %> 时发送后无法设置标头

  25. 25

    发送到客户端后无法设置标头 - Node/Mongoose/Express

  26. 26

    Node.js 344错误发送头后无法设置头

  27. 27

    Node.js 344错误发送头后无法设置头

  28. 28

    使用 mongoose 和 node.js 将标头发送到客户端后无法设置标头

  29. 29

    无法在node.js中设置标头

热门标签

归档