Express.js 服务器 .get() 在本地主机上没有响应

问题描述 投票:0回答:1

我目前正在学习 Web 开发并使用 Express.js 和 Node.js。我遇到的问题是当我尝试在网络浏览器中访问 http://localhost:3000/ 或 http://localhost:3001/ (我也尝试将端口更改为 3001)时,没有任何反应。我没有收到服务器的任何响应,“Test1”甚至没有在终端上打印/输出/记录。

我使用以下代码设置了一个简单的 Express 服务器:

import express from "express";
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  console.log("Test1");
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

我检查了服务器控制台,没有错误消息。

这就是资源管理器的样子: 探索者

这是我运行命令时终端显示的内容

node index.js
终端

这是 package.json 文件包含的内容: package.json

我有节点版本v22.1.0。安装在我的 wsl/vsCode 上。

有人可以帮我解决这个问题吗?我以前使用过类似的代码,我可以发誓它工作得很好,但现在它没有响应,我不确定是什么导致了问题。我花了几个小时尝试排除故障;这是解决问题的绝望尝试,哈哈。将不胜感激的帮助!

node.js express server localhost web-development-server
1个回答
0
投票

尝试

const express from "express";
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  console.log("Test1");
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

尝试使用Const而不是Import。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.