node.js - Node Express app.get ("/.."父目录不起作用

标签 node.js express

  1. ./core/server.js
  2. ./core/index.html
  3. ./core/styles.css
  4. ./bin/init-transpiled.js

如何在 localhost:8000(不是 /core)托管 index.html,其中 index.html 仍然可以访问相对路径 ../bin/init-transpiled./styles.css

最佳答案

您可以像这样托管您的index.html 文件。

app.get('/', function(req, res) {
    res.sendFile(path.resolve(__dirname + "/index.html"));
});

并且您需要在服务器配置中将 bin 文件夹设置为静态文件夹,才能从客户端访问 bin 文件夹文件。

app.use(express.static(path.resolve(__dirname + '/../bin')));   

然后在index.html中您可以像这样访问您的文件。

<script src="../bin/init-transpiled.js">

请注意,例如,如果索引使用 ./styles.css,则需要使用 ../core/styles.css

关于node.js - Node Express app.get ("/.."父目录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38820478/

相关文章:

javascript - 表达req.query.time 返回NaN

node.js - Nodejs Azure 存储 blob 服务 : getStats not working

javascript - 使用 Express Js 使用静态 html 页面引导数据

node.js - redis session 在服务器上不工作

javascript - Mongoose 子文档数组不保存整个对象?

node.js - 适用于 Nodejs 的 AWS 负载均衡器代理

javascript - 我如何使用 twilio 接收短信?

node.js - 添加霍根支持表达

javascript - 如何在 Node.js 中捕获原始请求

node.js - Node js错误 "process with a non-zero exit code"