gpt4 book ai didi

node.js - 无法获取/表达错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:53:13 26 4
gpt4 key购买 nike

我正在学习 Mean.js 堆栈,并尝试构建一个应用程序。我已经安装了 Express,并且可以正常工作。当我尝试配置我的静态文件(html、js、图像等)时,一切都崩溃了。

这是我的文件:

server.js

var express = require('express');
var app = express();
app.use(express.static(__dirname + "public"));

app.listen(3000);
console.log('Server running on port 3000');

我的 html 文件非常简单:

<!DOCTYPE>
<html>
<head>
<title>Contact List App</title>
</head>
<body>
<h1>Contact List App</h1>
</body>
</html>

因此,当我启动服务器时:node server.js,然后在浏览器中键入 http://localhost:3000/,我得到“Cannot获取”错误。

问题出在哪里?

enter image description here

最佳答案

__dirname 没有尾部斜杠,因此您需要在构建静态根目录时自行提供:

app.use(express.static(__dirname + "/public"));
^ this needs to be there

关于node.js - 无法获取/表达错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com