我想在我的节点 js 文件中创建一个虚拟路径前缀(该路径实际上并不存在于文件系统中),但它不起作用。
未创建虚拟路径,未从 index.html 加载我的 css 和 js
我只使用 app.js 和我也已经安装 express 的静态文件夹。
在我的 app.js 中这样的代码
app.use('/public', express.static(path.join(__dirname, 'static')));
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname,'static','index.html'));
});
app.listen(3000);
我想创建虚拟路径 '/public' 但不起作用,我的 'index.html' 无法加载我的 js 文件和 css 文件。这是我的 index.html 代码。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/static/css/main.css">
</head>
<body>
<h1>Some Html File </h1>
<script src="/static/js/main.js" type="text/javascript"></script>
</body>
</html>
如果我更改app.use('/public', express.static(path.join(__dirname, 'static')));
为app.use('/static', express.static(path.join(__dirname, 'static')));
它工作正常(我的 js 和 css 文件已加载)但我的虚拟路径未创建。
我哪里做错了?
谢谢