出于某种原因,它无法加载从我的 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/