我正在設定一個 node.js 服務器,但它沒有運行我的服務器檔案。服務器檔案的代碼:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
app.get('/', (req, res)=>{
res.status(200);
res.send("Welcome to root URL of Server");
});
app.listen(PORT =>{
console.log("hello world" PORT);
});
我的 json 檔案:
{
"name": "cp_viz",
"version": "1.0.0",
"description": "none",
"main": "index.js",
"scripts": {
"test": "none"
},
"repository": {
"type": "git",
"url": "none"
},
"keywords": [
"none"
],
"author": "none",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"nodemon": "^2.0.20"
}
}
每當我運行命令 node server.js 時,它都會移動到新的命令列并且不記錄任何內容。
uj5u.com熱心網友回復:
你應該寫:
app.listen(PORT, () => {
console.log("hello world" " ", PORT);
});
代替 :
app.listen(PORT =>{
console.log("hello world" PORT);
});
接下來安裝nodemon為開發依賴項,并在您的 package.jsonnpm install --save-dev nodemon檔案中使用此命令=>
"dev": "nodemon server.js"
并npm run dev在您的控制臺中運行!就是這樣!
服務器.js
const express = require("express");
const app = express();
const PORT = 3000;
app.use(express.json());
app.get("/", (req, res) => {
res.status(200);
res.send("Welcome to root URL of Server");
});
app.listen(PORT, () => {
console.log("hello world" " ", PORT);
});
輸出Visual Studio 代碼

在瀏覽器(本地主機)中輸出http://127.0.0.1:3000:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525211.html
下一篇:字串顯示未定義
