Created
February 19, 2013 18:04
-
-
Save klovadis/4988305 to your computer and use it in GitHub Desktop.
A basic webserver using the connect framework.
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
// 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