gpt4 book ai didi

javascript - 为什么我得到一个 404 js 文件,同时包含一个脚本标签?

转载 作者:行者123 更新时间:2023-11-30 15:14:22 25 4
gpt4 key购买 nike

index.html

<head>
<script src="/main.js"></script>
</head>

错误:

GET http://localhost:3000/main.js 

结构

  • 项目
    • 应用程序.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')));

之后你可以使用<script src="main.js"></script>在你的 index.html 中

关于javascript - 为什么我得到一个 404 js 文件,同时包含一个脚本标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684741/

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