Last active
July 31, 2018 19:20
-
-
Save RaschidJFR/aadfa9af6007dba9faf91654f43f5975 to your computer and use it in GitHub Desktop.
Basic webserver with NodeJS and Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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