./core/server.js
./core/index.html
./core/styles.css
./bin/init-transpiled.js
如何在index.html
(localhost:8000
)/core
仍然可以访问相对路径index.html
和../bin/init-transpiled
的情况下托管./styles.css
?
答案 0 :(得分:1)
您可以像这样托管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
答案 1 :(得分:0)
app.use('/', express.static(__dirname));
app.use('/', express.static(__dirname + '/../'));
app.get('/', function (req, res) {
res.sendFile(__dirname + "/index.html");
});
使用'/'
时
./styles.css
)../bin/
)index.html
作为回复。 注意: '/'
不是必需的,默认为'/'
,您可以将中间件处理函数编写为第一个参数< / p>