我正在按照平均堆栈中的课程进行操作,当我通过使用jwt令牌系统尝试身份验证部分时,每次发出请求时,npm服务器都开始关闭。当我尝试发出发布请求并将数据存储在db中时,数据将被存储,但是在该npm服务器关闭之后。
代码似乎没有任何错误。可以很好地存储数据,但发送和请求后,npm服务器会立即关闭
message.js
router.post('/', function(req, res, next) {
var decoded = jwt.decode(req.query.token)
User.findById(decoded.user._id, function(err, user){
if (err){
return res.status(500).json({
title:'An error ocured',
error:err
});
}
var message =new Message({
content:req.body.content,
user: user
});
message.save(function(err,result){
if(err){
return res.status(500).json({
title:'An error ocured',
error:err
});
}
user.messages.push(result);
user.save();
res.status(201).json({
message:'saved message',
obj:result
})
})
})
message.service.ts
addMessage(message:Message){
const body = JSON.stringify(message);
const headers = new Headers({'Content-Type':'application/json'});
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
:'';
return this.http.post('http://localhost:3000/message'+ token, body, {headers:headers})
.map ((response : Response) => {
const result=response.json()
const message= new Message(result.obj.content,'Dummy',result.obj._id,null)
this.messages.push(message)
return message;
} )
.catch((error: Response) => Observable.throw(error.json()));
}
package.json
{
"name": "nodejs-angular2",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"build": "del-cli public/js/app && webpack --config
webpack.config.dev.js --progress --profile --watch",
"build:prod": "del-cli public/js/app && ngc -p tsconfig.aot.json &&
ngc -p tsconfig.aot.json && webpack --config webpack.config.prod.js --
progress --profile --bail && del-cli 'assets/app/**/*.js'
'assets/app/**/*.ngsummary.json' 'assets/app/**/*.ngstyle.*'"
},
"dependencies": {
"@angular/animations": "^5.0.0",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/compiler-cli": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/platform-server": "^5.0.0",
"@angular/router": "^5.0.0",
"@angular/upgrade": "^5.0.0",
"bcryptjs": "^2.4.3",
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"core-js": "^2.4.1",
"debug": "~2.2.0",
"express": "~4.14.0",
"hbs": "~3.1.0",
"interpret": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.5.5",
"mongoose-unique-validator": "^2.0.3",
"morgan": "~1.6.1",
"reflect-metadata": "^0.1.3",
"rxjs": "^5.5.0",
"serve-favicon": "~2.3.0",
"zone.js": "^0.8.5"
},
"devDependencies": {
"@ngtools/webpack": "^1.8.0",
"@types/core-js": "0.9.36",
"@types/node": "^6.0.45",
"angular-router-loader": "^0.5.0",
"angular2-template-loader": "^0.5.0",
"awesome-typescript-loader": "^3.1.2",
"del-cli": "^0.2.0",
"html-loader": "^0.4.4",
"raw-loader": "^0.5.1",
"ts-loader": "^2.0.3",
"typescript": "^2.4.2",
"webpack": "^2.2.1",
"webpack-merge": "^4.1.0"
}
}
错误消息
ProgressEvent {isTrusted:true,lengthComputable:false,已加载:0,总计:0,类型:“ error”,…}
zone.js?fad3:3243 POST http://localhost:3000/message?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7Im1lc3NhZ2VzIjpbXSwiX2lkIjoiNWNmMmUzYjRjNTdiMDQ0NDE4YWRjZmY5IiwiZmlyc3ROYW1lIjoic2F0aGlyYSIsImxhc3ROYW1lIjoicGFzYW4iLCJwYXNzd29yZCI6IiQyYSQxMCR0b3pBNm1kU0dnT04xNDBmV3BmQUZ1dDcvZTQyQjR5Y1NFRUxvWXNxbWlwaEJLaUtaWW1BaSIsImVtYWlsIjoic2F0aGlyYUBwYXNhbi5jb20iLCJfX3YiOjB9LCJpYXQiOjE1NTk0MjI3MDAsImV4cCI6MTU1OTQyOTkwMH0.gKHQmIszqLyQrR3g-PKLjqyJz9MFEgU4hKhTT6boFMA net :: ERR_CONNECTION_RESET
答案 0 :(得分:0)
如果findById函数花费的时间太长,则NodeJS服务器所运行的线程可能会被阻塞。我通常使我的expressjs路由器功能异步并等待查询。尝试将async添加到post函数,然后等待findById函数。
router.post('/', async (req, res, next) => {
var decoded = jwt.decode(req.query.token)
await User.findById(decoded.user._id, function(err, user){ ... });
...
});