gpt4 book ai didi

node.js - 错误 : Can't set headers after they are sent , 仅在刷新页面后发生

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:31 26 4
gpt4 key购买 nike

每次我在路由“localhost:8080/”刷新 html 页面或登录用户时,我都会收到此错误:发送后无法设置 header 。据我所知发生此问题的原因是回调被意外调用了两次。该应用程序不会崩溃或发生任何事情,它只是在控制台上不断记录相同的错误。

这是代码。

var User        = require('../models/user');
var Story = require('../models/story');
var jwt = require('jsonwebtoken');
var config = require('../../config');


var superSecret = config.secret;


module.exports = function(app, express) {

// creating our first router
var apiRouter = express.Router();

// signup a user

apiRouter.post('/signup', function(req, res) {

var user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password
});

user.save(function(err) {
if(err) res.send(err);
res.json({ message: 'User has been created!' });
});
});

apiRouter.get('/users', function(req, res) {

User.find({}, function(err, users) {

if(err) res.send(err);

res.json(users);

});

});



// user login

apiRouter.post('/login', function(req, res) {

User.findOne({
username: req.body.username
}).select('name username password').exec(function(err, user) {

if(err) throw err;

if(!user) {
res.json({ message: "Wrong User" });
} else if(user) {

var validPassword = user.comparePassword(req.body.password);

if(!validPassword) {
res.json({ message: "Invalid Password" });

} else {

var token = jwt.sign({
id: user._id,
name: user.name,
username: user.username
}, superSecret, {
expiresInMinute: 1440
});

// return all the sucess
res.json({
success: true,
message: "Successfully login and token created!" + token.name,
token: token

});
};

}

});

});


// write a middleware for login
// A middleware for login user only, once a user has logged in , he can enter other links below
apiRouter.use(function(req, res, next) {

// do logging
console.log("Somebody just came to our app!");

var token = req.body.token || req.param('token') || req.headers['x-access-token'];

// check if token exist
if(token) {

jwt.verify(token, superSecret, function(err, decoded) {
if(err) {
res.status(403).send({ success: false, message: 'Failed to authenticate user' });
} else {

// if everything is good save request for use in other routes
req.decoded = decoded;

next();
}
});

} else {
res.status(403).send({ success: false, message: 'No token provided' });
}
});


apiRouter.route('/')

.post(function(req, res) {

var story = new Story({
user: req.decoded.id,
content: req.body.content
});

story.save(function(err, stor) {
if(err) res.send(err)
console.log(stor);
res.json(stor);
});

})


.get(function(req, res) {

Story.find({ user: req.decoded.id }, function(err, story) {
if(err) res.send(err);

res.json(story);
});


});




apiRouter.get('/me', function(req, res) {

res.send(req.decoded.name);

});

apiRouter.route('/:user_id')


.get(function(req, res) {

User.findById(req.params.user_id, function(err, user) {
if(err) res.send(err);

res.json(user);

});


})

.put(function(req, res) {

User.findById(req.params.user_id, function(err, user) {

if(err) res.send(err);

if(req.body.name) {
user.name = req.body.name;
}

if(req.body.username) {
user.username = req.body.username;

}

if(req.body.password) {
user.password = req.body.password;
}

user.save(function(err) {
if(err) res.send(err);

res.json({ message: "User updated" });

});


});

});

return apiRouter;

}

最佳答案

发生错误时您发送了两次响应:

if (err) res.send(err);
res.json(foo);

将其替换为:

if (err) { res.send(err); return; }
res.json(foo);

关于node.js - 错误 : Can't set headers after they are sent , 仅在刷新页面后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com