./core/server.js
./core/index.html
./core/styles.css
./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/