我在学习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/*.jpgor 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
更多推荐
发布评论