Skip to content

Instantly share code, notes, and snippets.

@klovadis
Created February 19, 2013 18:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klovadis/4988305 to your computer and use it in GitHub Desktop.
Save klovadis/4988305 to your computer and use it in GitHub Desktop.
A basic webserver using the connect framework.
// to install the connect framework, go to your applications directory
// and type "npm install connect" in your command prompt.
// to launch the webserver, place this file in your application
// directory and execute it using "node index.js"
var connect = require('connect')
, app = connect()
, webserver = require('http').createServer(app);
// lets you use req.body.xx for POST requests
app.use(connect.bodyparser());
// your routing function for your api
app.use(function (req, res, next) {
if (req.url === '/api/something') {
// do something
console.log('API call: ' + req.url)
return;
}
// no route matched -> continue with static file middleware
next();
});
// this middleware tries to resolve any requests that have not been
// handled in your routing middleware to files in the folder /static
app.use(connect.static(__dirname + '/static'));
// start listening on port 80
webserver.listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment