使用 Express 在 Node.js 上执行以下代码,但它不返回任何内容。看起来 res.send 在 promise.then() 中不起作用;看起来它已经在 promise resolve() 之前返回给调用者;我做错了什么?根据示例,它应该可以工作。谢谢。
const express = require('express');
const app = express();
app.get('/test', (req, res, next) => {
res.send("Good"); // Works Just Fine , but I dont need to return here
getMessage().then(function(data){
console.log("I'm here"); // Message works
res.send("Good"); // Does not Work
}).catch(function(error){
res.send("Error");
});
});
function getMessage(){
return new Promise(function(resolve, reject){
setTimeout(function() {
resolve();
}, 3000);
});
}
app.listen(PORT, () => {
console.log("run");
});
最佳答案
请在您的应用中添加以下代码:Refer Here
此应用启动服务器并在端口 8080 上监听连接
app.listen(8080, function () {
console.log('Example app listening on port 8080!')
})
关于javascript - Node.js Express 响应在 Promise 解析之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46870455/