Christopher Allen Christopher Allen - 1 year ago 36
Node.js Question

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');

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,
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.

max max
Answer Source

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 


with /html containing all of your static files.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download