【发布时间】:2019-02-09 23:05:32
【问题描述】:
我有一个如下的html页面
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Report</title>
<link rel="stylesheet" href="assets/app.css"/></head><body>
//Content//
<div id="report"></div><script src="assets/app.js"></script></body></html>
此 html 页面位于以下具有结构的文件夹中
report
--assets
--app.js
--app.css
--myfile.html
现在我的应用程序正在尝试呈现此 html。下面位于routes.js中,结构如下
app.use('/Users/report/assets', express.static('/Users/report/assets'));
app.get('/api/testresults/:id', (req, res) => {
const id = req.params.id;
res.sendFile('/Users/report/myfile.html');
});
我的应用结构如下
MyApp
app
—routes
—index.js
—routes.js
node_modules
package.json
server.js
我应该在这里添加什么。我不想更改 html,因为我相信一旦我使用 express.static() 它应该能够找到文件 我不断收到错误 app.js not found 404
注意:如果我将 html 的 href 更改为
/Users/report/assets/app.js
同样适用于 css,它可以工作,但我不想要。
【问题讨论】:
-
您没有显示
report和report/assets目录相对于您的应用程序文件结构的位置。 -
我相信你需要
express.static的相对路径 -
你去哪儿了?多人试图提供帮助,但您没有回应。
-
@All。谢谢您的帮助。当我给出绝对路径而不是相对路径时,该解决方案有效。我面临的问题是由于插件在尝试创建报告时未将“/”附加到资产。现在开始工作
标签: javascript html node.js express