使用node.js表达,如何从兄弟目录

时间:2018-02-12 03:53:40

标签: node.js express

背景:我已经尝试了感觉变量的每个组合,但我无法弄清楚如何让我的server.app文件使用兄弟目录来调用index.htm和其他文件。当它被设置为子目录时它工作正常,但我无法弄清楚如何让它作为兄弟姐妹工作。

问题:我的文件结构有什么问题或者快递中的来电?

文件夹结构:

名为www的目录包含两个兄弟姐妹的子目录。

目录#1 www/server/server.js

目录#2 www/html/index.htm

在server.js中我包含这两个命令。

app.use(express.static('www'));

app.get('/', (request, response) => {
    response.sendfile('/html/index.htm');
});

当我访问root时,我在浏览器中收到此错误。

Error: ENOENT: no such file or directory, stat '/html/index.htm

www/server/server.js

www/html/index.htm存在。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您需要将.htm文件重命名为.html。

即使他们是同一个express.static也不会寻找.htm

然后像这样使用它

app.use(express.static(__dirname + '/../html'));

app.get('/',(req, res) => res.sendFile('index.html'));