gpt4 book ai didi

node.js - Express.js index.js 404(未找到)

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

出于某种原因,它无法加载从我的 index.html 引用的 index.js 脚本。 server.js、index.html 和 index.js 都位于我的项目文件夹的根目录下。

server.js

var express = require('express'),
app = express();

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.get('/', function (req, res) {
res.render('index.html');
});

app.listen(4000, function () {
console.log('Example app listening on port 4000!');
});

index.html

<html>
</head></head>
<body>
<div class="ink-grid vertical-space">
<div id="content">
<div class="panel vertical-space">
<div id="app"/>
</div>
</div>
</div>
<script type="text/babel" src="index.js"></script>
</body>
</html>

最佳答案

您必须提供这些文件。您可以单独进行:

app.get('/index.js', function (req, res) {
res.sendFile('index.js');
});

或者为您要提供的所有内容创建一个目录(例如 www)并使用 express.static:

app.use(express.static('www'));

关于node.js - Express.js index.js 404(未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395631/

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