Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express Automatic Routing

I am building a Node.js application that has many .html files. Rather than having to go and specify a routing for each and every one of them, is there a way for the routing to be done automatically?

Ideally this would happen:

Request received for "/Test.html". It then checks a given folder for a file called "Test.html". I can optionally override this automatic wiring.

like image 257
Sebastian Patten Avatar asked Feb 14 '26 21:02

Sebastian Patten


1 Answers

I recommend using Express.js and it's static middleware:

app.configure(function(){
  ...
  var oneYear = 31557600000;
  app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
});

If a request to /test.html is made, then the static middleware looks into the "public" folder and checks if the file exists (if it exists it serves it).

like image 154
alessioalex Avatar answered Feb 17 '26 09:02

alessioalex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!