简体   繁体   中英

Folder path - Absolute folder path

I am trying to add static folder for a project in app.js file. Im using 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. Like this --> 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). My experience with node is limited to Linux, but I believe it ought to work the same with Windows...

Edit MacOS is actually a "Linux-like" OS, so the discussion of Linux below applies equally to 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. 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.

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.

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. 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 ).


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. Specifically, path.join method is provided to construct paths that work correctly on your operating system.

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 . 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.

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.

I believe,

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

This will serve ./public folder contents as static files. (default behaviour)

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

This will also serve the ./public folder, because you are merely escaping p (like escaping " by \\" ). As p and escaped p are same values it will work as expected.

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

This tries to serve /public folder. / 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.

You can test this by creating a directory called public in root and making a file inside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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