本地主机网页未在 Node.js 中加载且没有任何错误

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

我正在尝试运行我的本地主机node.js服务器,但它没有在任何浏览器中加载,我也没有收到任何错误,只是浏览器上有一个缓冲符号。我的代码在 VS Code 上运行良好。这是代码:

server.js代码:

const http = require("http");
const app = require("./app");

const port = 3000;
const server = http.createServer();

server.listen(port);


app.js代码:

const express = require("express");
const app = express();

app.use((req, res, next) => {
  res.status(200).json({
    message: "it works!",
  });
});

module.exports = app;

这不适用于任何 Node.js 代码

VS Code is running fine No error or nothing is being shown

我的 html 页面也发生了同样的事情,下面附有图像: HTML page when clicked on Go Live, but it works if i double click the file directly without vs code

请帮帮我

我尝试了很多方法,但它不起作用,我重新安装了node.js,我也重置了我的网络,我不明白这个问题。

javascript html node.js express localhost
1个回答
0
投票

在您的代码中,您应该将 Express 应用程序传递给 createServer

const http = require("http");
const app = require("./app");

const port = 3000;
const server = http.createServer(app);

server.listen(port);

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