简体   繁体   中英

How do I serve static files for my node-js server which are in my application root directory

Using app.use(express.static('public')) serves the files located under public folder

But if my files are outside the public folder in my application root directly how to serve them.

If you want to serve files in a folder outside of the current working directory, './../<dir_name>' is the way to go.

If you want to serve individual files instead of a directory, then you can use either,

app.use("/<some_url>", express.static(__dirname + '<path_to_file>')); 

or

res.sendFile('<path_to_file>');

or use a simple library like, https://www.npmjs.com/package/connect-static-file .

I recommend the first approach though.

Note: replace the <text> with your file names and path names as required.

You can use ./../ to go the parent of the current folder. If you have the following structure:

project
-   public
-   app_folder
--     app.js

You can use './../public' as the static folder.

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