[英]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.