我刚刚开始学习Node.js,但是我遇到了服务器问题。我尝试了各种解决方案,但是它们没有用(更改端口,更新了node.js,更新了chrome等),但没有任何效果。
服务器打开,但本地主机未响应。
这是我的index.js文件
const app = express();
const port = 3000;
app.get("/", (req, res) => {
console.log("test");
res.send("Hello world");
});
app.listen(port, () => {
console.log(`Server open port ${port}`);
});
这是我的package.json文件
"name": "last-server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
终端视图
$ node app.js
Server open port 3000
test
test
test
test
**服务器已启动,但未在Chrome中由localhost响应。相同的代码可以在我的朋友设备中使用。 **
答案 0 :(得分:1)
您很近。您导入模块了吗?
const express = require('express');
const port = 3003;
const app = express();
app.get("/", (req, res) => {
console.log("test");
res.send("Hello world");
});
app.listen(port, () => {
console.log(`Server open port ${port}`);
});