Reputation: 25763
This is the code that I see everywhere for a simple static web server using node.js and express:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8080);
My question is how does express know when the request is for a static page vs. a dynamic page? For example, why would /index.html be served from the public folder and not from dynamic templates?
Thanks.
Upvotes: 2
Views: 752
Reputation:
You can think of the routes you define as a chain of checks on the path defined in the URL. Express iterates one by one through each check looking for a match; once it finds one, it executes that callback.
In this case, express.static is defining a bunch of path checks for each file in the public
directory. If you app.use(express.static(__dirname + '/public'));
before your app.get('/index.html', function(req, res) { ...
code, and there is an index.html file in there, it will use the static file over the dynamic one.
That's basically it.
Upvotes: 1