Search code examples
javascriptnode.jsexpresswebserverurl-routing

Directory-Dynamic Node.JS express server


I am new with the whole web-server side of Node.JS, Unfortunately I cannot find how to make my express server serve dynamic web pages. I understand routes, and how they work.

Here's my code

    var express = require('express');
    var app = express();
    app.get('/', function (req, res){
res.send('Hello There From Express!\n');
    });
    app.listen('200');

This will only server the / directory, everything else will fail. How would i set this up where it will serve a dynamic webpage range, So like if the user wanted to type in http://example.com/billing.html, it would redirect to the billing.html file. I don't want to have to put in a routing line for each page, I would like to be able to just dynamically serve webpages..

Sorry if i'm not making any sense, i'm not the best at asking questions.


Solution

  • If you are wanting to just have express serve a folder of static HTML files without dealing with each one individually, you could define your app as:

    var express = require('express');
    var app = express();
    app.use(express.static(__dirname + '/html')); //where /html is a subfolder of your app 
    
    app.listen('200');
    

    with /html containing all of your static files.