简体   繁体   English

文件夹路径 - 绝对文件夹路径

[英]Folder path - Absolute folder path

I am trying to add static folder for a project in app.js file. 我想在app.js文件中为项目添加静态文件夹。 Im using macOS. 我正在使用macOS。

app.use(express.static("public"));   // will work
app.use(express.static("\public"));  // will work
app.use(express.static("/public"));  // wont work

Can someone tell me what is the difference between these three. 有人能告诉我这三者之间有什么区别。

"Public" folder is in project file. “Public”文件夹位于项目文件中。 Like this --> My_project > (public, app.js, node_modules) 像这样 - > My_project>(public,app.js,node_modules)

const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");

const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));

First, it's not clear which operating system you're using. 首先,不清楚您正在使用哪种操作系统。 From your second example, it looks like you're using Windows (since that's the only OS that uses the "\\" character as a directory separator). 从您的第二个示例,它看起来像您正在使用Windows(因为这是唯一使用“\\”字符作为目录分隔符的操作系统)。 My experience with node is limited to Linux, but I believe it ought to work the same with Windows... 我对节点的体验仅限于Linux,但我相信它应该与Windows一样......

Edit MacOS is actually a "Linux-like" OS, so the discussion of Linux below applies equally to MacOS. 编辑 MacOS实际上是一个“类似Linux”的操作系统,所以下面对Linux的讨论同样适用于MacOS。

So, to answer your first question about the three different "public" paths: 那么,回答关于三种不同“公共”路径的第一个问题:

app.use(express.static("public")); will use the public directory relative to where you started your node application. 将使用相对于您启动节点应用程序的public目录。 So, if you're in the directory "src" and your "public" directory is in the "src" directory, then it will serve files from that "public" directory. 因此,如果您在“src”目录中并且“public”目录位于“src”目录中,那么它将提供来自该“public”目录的文件。

app.use(express.static("\\public")); will (I think) work the same as with the previous example. 将(我认为)与前一个例子的工作方式相同。 This is using the Windows path separator, so this is behavior I'm not too familiar with. 这是使用Windows路径分隔符,所以这是我不太熟悉的行为。

app.use(express.static("/public")); will attempt to serve public files from a "public" directory located at the root of the filesystem on a Linux system. 将尝试从位于Linux系统上文件系统根目录的“公共”目录中提供公共文件。 This is actually an unusual use - normally you wouldn't create a public file directory in your filesystem root. 这实际上是一种不常见的用法 - 通常您不会在文件系统根目录中创建公共文件目录。 You would be more likely to use the first version (or a slightly different path specification that means the same thing, like ./public ). 您更有可能使用第一个版本(或稍微不同的路径规范,意味着相同的东西,如./public )。


So your example gives three different path specifications with varying meanings on different operating systems. 因此,您的示例给出了三种不同的路径规范,这些规范在不同的操作系 How can you avoid this confusion? 你怎么能避免这种混乱?

Node provides a solution to this kind of confusion: the path module. Node提供了这种混淆的解决方案: 路径模块。 Specifically, path.join method is provided to construct paths that work correctly on your operating system. 具体来说,提供了path.join方法来构造在您的操作系统上正常工作的路径。

One pattern which I have seen used a lot is to make sure you are getting exactly the file/directory you expect by making it relative to the current file, like so: 我见过的一个模式是通过使它相对于当前文件来确保你正好得到你期望的文件/目录,如下所示:

app.use(express.static(path.join(__dirname, 'public')));

Imagine that this is in a file called C:\\development\\myproject\\index.js and your static assets are in C:\\development\\myproject\\index.js . 想象一下,这是在一个名为C:\\development\\myproject\\index.js ,您的静态资产位于C:\\development\\myproject\\index.js This code will wind up working like this: 这段代码最终会像这样工作:

app.use(express.static('C:\development\myproject\public'));

where the path is constructed via the path.join method. 路径是通过path.join方法构造的。

And, if you switch to Linux in the future (or you have other developers working on the project on their Linux machines), the code doesn't need to be changed to update the path. 而且,如果您将来切换到Linux(或者您的Linux机器上有其他开发人员在处理该项目),则无需更改代码来更新路径。

I believe, 我相信,

app.use(express.static("public"));

This will serve ./public folder contents as static files. 这将把./public文件夹内容作为静态文件提供。 (default behaviour) (默认行为)

app.use(express.static("\\public"));

This will also serve the ./public folder, because you are merely escaping p (like escaping " by \\" ). 这也将服务于./public文件夹,因为你只是转义p (就像转义为" \\" )。 As p and escaped p are same values it will work as expected. 由于p和转义p是相同的值,它将按预期工作。

app.use(express.static("/public"));

This tries to serve /public folder. 这会尝试提供/public文件夹。 / without any . /没有任何. means root, so it will try to access your local drive root directory and check if there is a public directory inside it. 表示root,因此它将尝试访问您的本地驱动器根目录并检查其中是否有public目录。

You can test this by creating a directory called public in root and making a file inside it. 您可以通过在root创建名为publicroot并在其中创建文件来对此进行测试。

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

相关问题 如何使用 ESLint 通过特定文件夹的绝对路径限制模块的导入 - How to restrict the import of the module by absolute path for the particular folder with ESLint 如何在 Windows 10 和 nodejs 中获取 tmp 文件夹的绝对路径? - How get absolute path for tmp folder in windows 10 and nodejs? 错误:绝对路径://fileName/ 解压 protractor nodejs 中的文件夹 - Error: absolute path: //fileName/ unzipping a folder in protractor nodejs 如何在此swf目标代码中定义xml,images文件夹和主swf路径的绝对路径? - How to define absolute path of xml, images folder and main swf path in this swf object code? AJAX文件夹路径问题 - AJAX folder path issue 拆分文件夹路径字符串 - Split string of folder path 如何在电子,node.js或html / javascript中获取GUI选择的文件夹的绝对路径 - How to get GUI-selected folder's absolute path in electron, node.js, or html/javascript 如何从nodeJS中的子文件中获取与父文件夹相同目录中的文件的绝对路径 - how to get the absolute path of a file in the same the same directory as the parent folder, from the child file in nodeJS 正则表达式从路径中排除文件夹 - Regex Exclude folder from path 从路径中删除最后一个文件夹 - remove the last folder from a path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM