【问题标题】:I cant create a virtual path prefix (where the path does not actually exist in the file system) in node js using express我无法使用 express 在节点 js 中创建虚拟路径前缀(文件系统中实际上不存在该路径)
【发布时间】:2020-01-09 04:31:50
【问题描述】:

我想在我的 node js 文件中创建一个虚拟路径前缀(该路径实际上并不存在于文件系统中),但它不起作用。

虚拟路径未创建,我的 css 和 js 未从 index.html 加载

我只使用 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 文件已加载)但我的虚拟路径未创建。

我哪里做错了?

谢谢

【问题讨论】:

    标签: javascript node.js express path virtual-path


    【解决方案1】:

    我的错在 index.html 中有一些遗漏,它应该是我在 app.js 中创建的带有 /public(虚拟路径)的 href 和 src

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 2016-11-13
      • 2023-04-01
      • 2019-12-17
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多