Last active
December 24, 2015 15:09
-
-
Save edm00se/6817424 to your computer and use it in GitHub Desktop.
NodeJS simple web app/server environment (using express, express's compress and prerender-node packages) set to serve out static content in root directory. For when you need a simple web server without the bloat.
This file contains hidden or 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
//set up the app | |
var express = require('express'); | |
var app = express(); | |
//enables express-toobusy, which keeps it from melting under HIGH pressure | |
//app.use(require('express-toobusy')()); | |
//enables compress and prerender-node | |
//app.use(express.compress()); | |
//app.use(require('prerender-node')); | |
//setting cache-control | |
//var aYear = 31536000000; //365*aDay | |
//var aMonth = 2592000000; //30*aDay | |
var aDay = 86400000; //1000ms*60s*60min*24hr | |
//establishes path of static files in root directory | |
app.use(express.static(__dirname + "/public", { maxAge: aDay })); | |
//establish simple text catch for Error 500 and Error 404 | |
app.use(function(err, req, res, next){ | |
console.error(err.stack); | |
res.send(500, "Something broke!"); | |
}); | |
app.use(function(err, req, res, next){ | |
res.send(404, "Sorry, can't find that!"); | |
}); | |
//set to listen on environment port or port 3000, if no detected environment port | |
var portToListenOn = process.env.PORT || 3000; | |
app.listen(portToListenOn); | |
console.log("NodeJS serving content from "+__dirname+"/public/ on localhost:"+portToListenOn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment