Node.js无服务器脱机在第一次请求后挂起
当我第一次发出请求时,它工作正常,但是当我再次发出请求而不重新启动服务器时,它将超时并返回一些承诺错误Node.js无服务器脱机在第一次请求后挂起,node.js,express,serverless-framework-offline,serverless-plugins,Node.js,Express,Serverless Framework Offline,Serverless Plugins,当我第一次发出请求时,它工作正常,但是当我再次发出请求而不重新启动服务器时,它将超时并返回一些承诺错误 following is the code and error module.exports.getOne = (event, context, callback) => { context.callbackWaitsForEmptyEventLoop = false; db.once('open', () => { Client.findOne({name
following is the code and error
module.exports.getOne = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
db.once('open', () => {
Client.findOne({name:{ $regex : new RegExp(event.pathParameters.name, "i") }})
.then(client => callback(null, {
statusCode: 200,
body: JSON.stringify(client)
}))
.catch(err => callback(null, {
statusCode: err.statusCode || 500,
headers: { 'Content-Type': 'text/plain' },
body: 'Could not fetch the note.'
}))
.finally(() => {
// Close db connection or node event loop won't exit , and lambda will timeout
db.close();
});
});
};
Error:
(node:3273) UnhandledPromiseRejectionWarning: Error: Cannot stop server while in stopping state
问题是您没有通过调用
callback
来完成调用,因此它认为您的getOne()函数没有完成
查看serverless在使用其模板设置新的serverless应用程序时提供的示例代码
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
callback(null, response);
};