Skip to content

Instantly share code, notes, and snippets.

@stevenklise
Last active December 10, 2015 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenklise/4489330 to your computer and use it in GitHub Desktop.
Save stevenklise/4489330 to your computer and use it in GitHub Desktop.
How to include Ecstatic https://github.com/jesusabdullah/node-ecstatic with http.createServer() and other routes.
var http = require('http');
// Set up ecstatic to read from the root folder of this app.
var ecstatic = require('ecstatic')(__dirname);
var matchRoutes = function (req, res) {
console.log('Request: '+ req.method + ' ' + req.url)
// Match the main route "/"
if (req.url === "/" ) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n on '+ req.url);
// Match all routes with just the letters a through z. No punctuation, no numbers.
} else if ( /^\/([a-z]*)$/.test(req.url) === true ) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n on '+ req.url);
// in all other cases, try and find a file matching req.url
} else {
ecstatic(req,res)
}
}
http.createServer(matchRoutes).listen(3000);
console.log('Listening on localhost:3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment