Created
November 20, 2010 20:18
-
-
Save indexzero/708119 to your computer and use it in GitHub Desktop.
An example of using Journey with Node-Static in node.js
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
var sys = require('sys'), | |
http = require('http'), | |
static = require('node-static'), | |
journey = require('journey'); | |
exports.createRouter = function () { | |
return new (journey.Router)(function (map) { | |
// | |
// Version Binding | |
// | |
map.get('/version').bind(function (response) { | |
response.send(200, { 'Content-Type': 'text/html' }, { version: journey.version.join('.') }); | |
}); | |
// | |
// Put your journey routes here... | |
// | |
}, { strict: false }); | |
}; | |
exports.createServer = function (options) { | |
var router = exports.createRouter(), | |
files = new (static.Server)('./public'); | |
var server = http.createServer(function (req, res) { | |
var body = ''; | |
// Append the chunk to body | |
req.addListener('data', function (chunk) { | |
body += chunk; | |
}); | |
req.addListener('end', function () { | |
// | |
// Dispatch the request to the router | |
// | |
router.route(req, body, function (route) { | |
if (route.status === 404) { | |
// Log the 404 | |
sys.puts('Router did not match request for: ' + req.url); | |
files.serve(req, res, function (err, result) { | |
// If the file wasn't found | |
if (err && (err.status === 404)) { | |
res.writeHead(404); | |
res.end('File not found.'); | |
} | |
}); | |
return; | |
} | |
res.writeHead(route.status, route.headers); | |
res.end(route.body); | |
}); | |
}); | |
}); | |
if (options.port) server.listen(options.port); | |
return server; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment