Javascript Can';t链接到Express提供的HTML样式表或脚本
我正在尝试将我的Express应用程序的代码拆分为多个文件。但是,我不能使用Javascript Can';t链接到Express提供的HTML样式表或脚本,javascript,html,css,node.js,express,Javascript,Html,Css,Node.js,Express,我正在尝试将我的Express应用程序的代码拆分为多个文件。但是,我不能使用或链接到样式表或脚本 以下是my index.js中的相关代码: const express = require('express'); const app = express() app.get('/', (req, res) => { res.sendFile(__dirname + '/public/index.html'); }); 我的文件结构是: . ├── index.js └── publi
或
链接到样式表或脚本
以下是my index.js中的相关代码:
const express = require('express');
const app = express()
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
我的文件结构是:
.
├── index.js
└── public
├── favicon.ico
└── index.html
有人能帮忙吗?您有:
因此,当浏览器请求/
时,您将发送index.html
这就是你所拥有的一切
如果浏览器要求任何内容,否则您将向其发送404错误页面
如果要提供静态文件,请使用static
模块(其中包括使用显式路由提供的index.html文件)
这是一本书
您的express程序只提供
public/index.html
。您没有任何代码提供样式表或脚本。@Quentin您可以使用res.sendFile提供样式表吗?可以。这是个坏主意,但你可以。非常感谢你!
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.use(express.static(__dirname + '/public'))