Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问Node.js中当前文件夹之外的文件_Javascript_Node.js - Fatal编程技术网

Javascript 访问Node.js中当前文件夹之外的文件

Javascript 访问Node.js中当前文件夹之外的文件,javascript,node.js,Javascript,Node.js,我试图在主服务器的文件夹中创建一个Node.js路由模块,如下所示 +root.js (server main) +modulos (folder where rutas.js is) ++rutas.js +shalimar (folder where all the static files are) ++muestrame.html (I want to send this) 我的代码如下所示: root.js(主服务器) var express=需要(“express

我试图在主服务器的文件夹中创建一个Node.js路由模块,如下所示

+root.js (server main)
+modulos (folder where rutas.js is)
    ++rutas.js
+shalimar (folder where all the static files are)
    ++muestrame.html (I want to send this)
我的代码如下所示:

root.js(主服务器)
var express=需要(“express”);
var Routeru app=require('./模/rutas');
listen(4200,函数(){
console.log('Servidor Web Iniciado en el-Puerto:4200');
});
使用(express.static(uu dirname+'/shalimar');

使用('/app',路由器应用程序)
是的,
\uu dirname
是当前文件夹的值,因此它仍然是相对于
模的。您必须将该值存储到一个变量中,并从根目录中的索引文件中传递它

为什么不直接做
“../shalimar/muestrame.html”


或者更好的
path.resolve('../shalimar/muestrame.html')

是的,
\uu dirname
是当前文件夹的值,因此它仍然是相对于
模块的。您必须将该值存储到一个变量中,并从根目录中的索引文件中传递它

为什么不直接做
“../shalimar/muestrame.html”


或者更好的
path.resolve('../shalimar/muestrame.html')

如果源文件不在项目中,可能需要根据文件系统的层次结构全局指定其路径,而不是使用相对路径(如果源文件不在项目中),也许您必须根据文件系统的层次结构全局指定他的路径,而不是使用相对路径。没有理由使用
path.normalize()
。这样做没有坏处,以确保操作系统之间没有不一致。也许可以改为
path.resolve()
。express核心代码似乎在
sendFile
本身中也没有使用,但它将发送到其中的文件名包装在
路径中。resolve()
。/xxx
之间没有操作系统不一致之处。不过,Unix/Windows之间肯定存在向前/向后斜杠?因此,现在您的意思是,在node.js编程中使用的每个路径名都应该通过
path.normalize()
?只是好奇,如果您认为这是必需的?没有理由使用
path.normalize()。也许可以改为
path.resolve()
。express核心代码似乎在
sendFile
本身中也没有使用,但它将发送到其中的文件名包装在
路径中。resolve()
。/xxx
之间没有操作系统不一致之处。不过,Unix/Windows之间肯定存在向前/向后斜杠?因此,现在您的意思是,在node.js编程中使用的每个路径名都应该通过
path.normalize()
?只是好奇,如果你认为这是必需的?