简体   繁体   English

express.static根路径的规则是什么?

[英]What is the rule for express.static root path?

I ran 我跑了

node src/app.js

in the mean-to directory 在均值目录中

express.static('public') 

would work,why? 会工作,为什么呢?

don't need to specify the path? 不需要指定路径?

what's the rule? 有什么规则?

and I know 而且我知道

__dirname+'/../public'` 

works just fine. 效果很好。

在此处输入图片说明


Just want to make sure the the logic here 只想确保这里的逻辑

I look up the doc http://expressjs.com/en/starter/static-files.html 我在http://expressjs.com/en/starter/static-files.html上查找了文档

it says 它说

"Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. " “将包含静态资产的目录的名称传递给express.static中间件函数,以开始直接提供文件。”

"the path that you provide to the express.static function is relative to the directory from where you launch your node process " “您提供给express.static函数的路径是相对于您启动节点进程的目录的

Does that mean 这是否意味着

  1. if I run node src/app.js in mean-to folder --> use express.static('public') 如果我在均值文件夹中运行节点src / app.js- >使用express.static('public')
  2. if I run node app.js in src folder => use express.static('../public') 如果我在src文件夹中运行节点app.js =>使用express.static('../ public')

and for safety, better use __dirname to get the absolute path of the directory 为了安全起见,最好使用__dirname获取目录的绝对路径

Express static uses resolve function from module path Express static使用模块path resolve函数

Example using path : 使用path示例:

var p = require('path');
p.resolve('public'); // will return your absolute path for `public` directory

So rules for express static the same as for path#resolve function 因此,用于表达静态的规则与用于path#resolve函数的规则相同

You can see more example in the docs for path module 您可以在文档中查看路径模块的更多示例


What's the difference between p.resolve and __dirname ? p.resolve__dirname什么__dirname

path#resolve 路径#解析

p.resolve() resolves to an absolute path p.resolve()解析为绝对路径

path.resolve('public');

Will return your absolute path for public ' directory(eg " C:\\projects\\myapp\\public ") 将返回public '目录的绝对路径(例如“ C:\\projects\\myapp\\public ”)

path.resolve('src'); // => "C:\projects\myapp\src")

__dirname __dirname

__dirname - name of the directory(absolute path) from which you're currently running your app __dirname您当前从中运行应用程序的目录名称(绝对路径)

file app.js 文件app.js

console.log(__dirname);

Running node app.js will print your absolute path for your app, eg " C:\\projects\\myapp " 运行node app.js将为您的应用程序打印您的绝对路径,例如“ C:\\projects\\myapp

You mixed 2 lines in one... And both are necessary: 您将两行混合在一起...两者都是必要的:

//This line is to say that the statics files are in public
app.use(express.static(__dirname + "/public")); 

//This another line is to say that when call the route "/" render a page
app.get("/", function (req, res) {
  res.send('Hello World!'); 
}

Probably, the reason is in running it from directory highter one level than dir with code? 可能是因为在目录中使用代码比dir从目录更高一级运行它? Try run it from src: 尝试从src运行它:

cd src
node app.js

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

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