admin 管理员组文章数量: 1086019
I've been following the tutorial here and it's been fine up until the part where I had to run the server:
If I try and run the code below I get server is listening on undefined
const app = express();
const port = 8080;
app.get("/", (req, res) => {
res.send("Hello");
})
app.listen((port, err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
I even tried to set the port beforehand using app.set('port', port);
const app = express();
const port = 8080;
app.set('port', port);
app.get("/", (req, res) => {
res.send("Hello");
})
app.listen((port, err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
But the same thing happens.
I've been following the tutorial here and it's been fine up until the part where I had to run the server: https://www.digitalocean./munity/tutorials/setting-up-a-node-project-with-typescript
If I try and run the code below I get server is listening on undefined
const app = express();
const port = 8080;
app.get("/", (req, res) => {
res.send("Hello");
})
app.listen((port, err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
I even tried to set the port beforehand using app.set('port', port);
const app = express();
const port = 8080;
app.set('port', port);
app.get("/", (req, res) => {
res.send("Hello");
})
app.listen((port, err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
But the same thing happens.
Share Improve this question asked Dec 22, 2020 at 14:28 jm123456jm123456 6351 gold badge10 silver badges22 bronze badges3 Answers
Reset to default 8app.listen
accept two arguments, first one is port, second is callback.
Change it to following code.
app.listen(port, (err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
This is an example in tutorial. He ignores the ()
in function from (err) => {}
to err => {}
, both work.
app.listen(port, err => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
app.listen(port, (err) => {
if (err) {
return console.error(err);
}
return console.log(`server is listening on ${port}`);
});
this is wrong, because app.listen()
does not take "err". Its callback does not take any argument. If you were using typescript, you would get warning. If app.listen()
gets an error, it will not run and client will get 404 error. it is just like this:
app.listen(port,()=>{
console.log(`server is listening on ${port}`)})
change it with following code , first params will be port
app.listen(port, err) => {
if (err)return console.error(err);
return console.log(`server is listening on ${port}`);
});
本文标签: javascriptExpress port is undefined when running with quotnode quotStack Overflow
版权声明:本文标题:javascript - Express port is undefined when running with "node ." - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744006782a2517432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论