为什么我得到一个 404 js 文件,同时包含一个脚本标签?
Why am i getting a 404 js file, while including a script tag?
index.html
<head>
<script src="/main.js"></script>
</head>
错误:
GET http://localhost:3000/main.js
结构
- 项目
- app.js
- 查看
- index.html
- main.js
我试过 src="main.js"。 /view/main.js
非常基础,但不想再卡在这个上面了...唉。
如果它对我的 app.js 文件有帮助:
app.get('/', function (req, res) {
res.sendFile(__dirname + '/view/home.html');
});
因此,根据您的评论 - 您只提供 'index.html' 文件而不是整个目录。
试试这个代码:
var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'view')));
//... other settings and server launching further
如果您想将服务静态文件设置为特定路由 - 使用“/your-route”扩展 'app.use' 行,如下所示:
app.use('/your-route', express.static(path.join(__dirname, 'view')));
之后您可以在 index.html
中使用 <script src="main.js"></script>
index.html
<head>
<script src="/main.js"></script>
</head>
错误:
GET http://localhost:3000/main.js
结构
- 项目
- app.js
- 查看
- index.html
- main.js
我试过 src="main.js"。 /view/main.js
非常基础,但不想再卡在这个上面了...唉。
如果它对我的 app.js 文件有帮助:
app.get('/', function (req, res) {
res.sendFile(__dirname + '/view/home.html');
});
因此,根据您的评论 - 您只提供 'index.html' 文件而不是整个目录。 试试这个代码:
var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'view')));
//... other settings and server launching further
如果您想将服务静态文件设置为特定路由 - 使用“/your-route”扩展 'app.use' 行,如下所示:
app.use('/your-route', express.static(path.join(__dirname, 'view')));
之后您可以在 index.html
中使用<script src="main.js"></script>