Mercurial > eldonilo > blog
diff server.js @ 6:29becdd02dea
Fix server file handling.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 19 Sep 2011 02:13:42 -0700 |
parents | f630f4b90564 |
children | 7ab6b48122af |
line wrap: on
line diff
--- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ var config = require('./configuration'); var util = require('util'); var http = require('http'); +var parseurl = require('url').parse; var fs = require('fs'); var xmpp = require('node-xmpp'); var Element = xmpp.Element; @@ -352,7 +353,6 @@ var home = function(res, title, desc, bo } res.write(' </body>\n'); - console.log('It works!'); res.end('</html>\n'); }; @@ -360,15 +360,15 @@ http.createServer(function(req, res) { util.log('Connection from ' + (req.headers['x-forwarded-for'] || req.client.remoteAddress) + ' (' + req.headers['user-agent'] + ') to ' + req.method.toLocaleLowerCase() + ' “' + req.url + '” from ' + req.headers['referer'] + '.'); var re = new RegExp('^' + config.webRoot); - req.url = req.url.replace(re, ''); - var ext = req.url.substring(req.url.lastIndexOf('.')+1); - var url = require('url').parse(req.url); + var url = parseurl(req.url.replace(re, '')); + var pathname = url.pathname || ''; + var ext = pathname.substring(pathname.lastIndexOf('.')+1); - if (url.pathname === '') + if (pathname === '') return servePage(url, res); - fs.readFile(req.url, function(err, data) { - if (err) + fs.readFile(pathname, function(err, data) { + if (err || pathname === 'index.xhtml') return servePage(url, res); res.writeHead(200, {'Content-Type': config.types[ext] || 'application/octet-stream'});