简体   繁体   English

如何为我的node-js服务器提供静态文件,这些文件位于应用程序根目录中

[英]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 使用app.use(express.static('public'))提供位于公共文件夹下的文件

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. 如果要在当前工作目录之外的文件夹中提供文件,则可以使用'./../<dir_name>'

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 . 或使用简单的库,例如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. 注意:根据需要,将<text>替换为您的文件名和路径名。

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. 您可以使用'./../public'作为静态文件夹。

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

相关问题 如何将某些Node.js文件放置在Node.js应用程序根文件夹之外? - How can I place some of my Node.js files outside of my Node.js application root folder? Webpack / node.js构建,webpack-dev-server是否可以将文件提供到我的公共目录? - Webpack/node.js build, is it possible for webpack-dev-server to serve files to my public directory? 如何从Node.js / Express.js中的CDN提供我的根页面? - How can I serve my root page from a CDN in Node.js / Express.js? 我的简单Express Server不会提供静态JS文件 - My simple Express Server won't serve static JS files 如何在节点JS服务器中包含根CA证书? - How do I include a root CA certifciate in my node JS server? 如何在本地通过Node.js提供静态文件? - How do I serve static files through Node.js locally? 如何从节点/ Express服务器提供Vuejs前端文件? - How can I serve my Vuejs front end files from my node/express server? 放置节点js应用程序的webapp文件的服务器目录 - Which server directory to place webapp files for a node js application 如何在以下条件下提供我的静态文件 - how to serve my static files in condition below 如何将发布数据发送到服务器端node.js应用程序? - How do I send post data to my server side node.js application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM