ExpressJS应用程序(ExpressJS app.use)

编程入门 行业动态 更新时间:2024-10-25 20:22:49
ExpressJS应用程序(ExpressJS app.use)

我在学习ExpressJS,并且遇到了这段代码。 我似乎无法理解app.use函数,并且文档对我来说还不清楚。 当app.use被调用时,这个特定示例代码中/ public目录发生了什么?

// Require dependencies var express = require('express'); var app = express(); // Set server port app.set('port', (process.env.PORT || 3000)); // Set static page directory, /public app.use(express.static(__dirname + '/public')); app.use('/public', express.static('public')); // Set template file directory, /views. Set view engine to EJS app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); // Route root request to pages/index app.get('/', function(request, response) { response.render('pages/index'); }); // Route favicon request to public/favicons app.get('/favicon.ico', function(request, response) { response.render('./public/favicons'); }); // Begin listening at specified port app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); });

I'm trying to learn ExpressJS and I came across this piece of code. I just can't seem to understand the app.use function and the documentation is unclear to me. What exactly is happening to the /public directory in this particular example code when app.use is called?

// Require dependencies var express = require('express'); var app = express(); // Set server port app.set('port', (process.env.PORT || 3000)); // Set static page directory, /public app.use(express.static(__dirname + '/public')); app.use('/public', express.static('public')); // Set template file directory, /views. Set view engine to EJS app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); // Route root request to pages/index app.get('/', function(request, response) { response.render('pages/index'); }); // Route favicon request to public/favicons app.get('/favicon.ico', function(request, response) { response.render('./public/favicons'); }); // Begin listening at specified port app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); });

最满意答案

这很简单 - 你设置的公共目录可以通过HTTP访问。

所以,像http://localhost:3000/public/abc.jpg这样的东西会给你公用文件夹中的abc.jpg 。

app.use('/public', express.static('public'))

线意味着 - 匹配任何以/public开头的路径,如:

http://localhost/public/*.jpg

或任何其他扩展名 - 将从public选择该文件( express.static('public') )文件夹中的参数并提供该文件。

该线

app.use(express.static(__dirname + '/public'))

意思是 - 匹配任何路径,如果在public目录中找到文件,则通过HTTP提供。

你可以使用这两行 - 不同之处在于URL中的/public部分。

这些文档非常清楚: https : //expressjs.com/en/starter/static-files.html

It's simple - you are setting up the public directory to be accessible over HTTP.

So, something like http://localhost:3000/public/abc.jpg will give you the abc.jpg from the public folder.

The

app.use('/public', express.static('public'))

line simply means - match any path that starts with /public like:

http://localhost/public/*.jpg

or any other extension - will choose that file from your public (the argument in express.static('public')) folder and serve it.

The line

app.use(express.static(__dirname + '/public'))

means - match any path and if file found in public directory, serve it over HTTP.

You can just use of the these two lines - difference being the /public part in the URL.

The docs are quite clear about this: https://expressjs.com/en/starter/static-files.html

更多推荐

本文发布于:2023-07-22 18:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   ExpressJS   app

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!