Skip to content

Instantly share code, notes, and snippets.

@RaschidJFR
Last active July 31, 2018 19:20
Show Gist options
  • Save RaschidJFR/aadfa9af6007dba9faf91654f43f5975 to your computer and use it in GitHub Desktop.
Save RaschidJFR/aadfa9af6007dba9faf91654f43f5975 to your computer and use it in GitHub Desktop.
Basic webserver with NodeJS and Express
#!/usr/bin/env node
// This script runs a simple server for HTML, CSS, JS files.
const publicFolder = 'www'; // Change this folder to point to your web files
const port = 8080; // Default port for web server but you can change it
// (Make sure you've installed the required dependencies)
const express = require('express');
const path = require('path');
const app = express();
app.get(/.*/, function (req, res) {
let isRoot = path.basename(req.path) == '';
let output = __dirname + publicFolder +'/' + req.path + (isRoot ? '/index.html' : '');
res.sendFile(path.join(output));
});
app.listen(port, (err) => {
if (err) {
return console.log(err)
}
console.log(`server listening on ${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment