简体   繁体   English

如何更改此功能以仅在express上给我项目文件夹路径?(__ dirpath)

[英]How can i change this function to only give me the project folder path on express?(__dirpath)

app.use(function(req, res) {
   res.sendFile(__dirname + '/views/404.html');
});

The current fucntion is giving me this path ...\\Ex1\\server\\views\\404.html how can i change the function to give me this path Ex1\\views\\404.html and send the html file. 当前的功能是给我这个路径... \\ Ex1 \\ server \\ views \\ 404.html如何更改函数以给我这个路径Ex1 \\ views \\ 404.html并发送html文件。

在此处输入图片说明

app.use(function(req, res) {
  const path = require('path');
  const fpath = path.normalize(__dirname + "/..");
  res.sendFile(fpath + '/views/404.html');
});

To resolve my problem i had to get the "path" module https://nodejs.org/docs/latest/api/path.html and use the path.normalize method. 为了解决我的问题,我必须获取“路径”模块https://nodejs.org/docs/latest/api/path.html并使用path.normalize方法。

path.normalize(__dirname + "/.."); path.normalize(__ dirname +“ / ..”);

where __dirname is "\\Ex1\\server" 其中__dirname是“ \\ Ex1 \\ server”

Using __dirname + "/.." in path.normalize it will return only the Ex1 then i added "/views/404.html" to return the file i pretended. 在path.normalize中使用__dirname +“ / ..”它将仅返回Ex1,然后我添加了“ /views/404.html”以返回我假装的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM