comparison 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
comparison
equal deleted inserted replaced
5:4d7a67349089 6:29becdd02dea
4 4
5 var config = require('./configuration'); 5 var config = require('./configuration');
6 6
7 var util = require('util'); 7 var util = require('util');
8 var http = require('http'); 8 var http = require('http');
9 var parseurl = require('url').parse;
9 var fs = require('fs'); 10 var fs = require('fs');
10 var xmpp = require('node-xmpp'); 11 var xmpp = require('node-xmpp');
11 var Element = xmpp.Element; 12 var Element = xmpp.Element;
12 var JID = require('./jid'); 13 var JID = require('./jid');
13 var ns = require('./ns'); 14 var ns = require('./ns');
350 res.write('\n'); 351 res.write('\n');
351 res.write(' <script type="application/ecmascript" src="blog.js"/>\n'); 352 res.write(' <script type="application/ecmascript" src="blog.js"/>\n');
352 } 353 }
353 354
354 res.write(' </body>\n'); 355 res.write(' </body>\n');
355 console.log('It works!');
356 res.end('</html>\n'); 356 res.end('</html>\n');
357 }; 357 };
358 358
359 http.createServer(function(req, res) { 359 http.createServer(function(req, res) {
360 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'] + '.'); 360 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'] + '.');
361 361
362 var re = new RegExp('^' + config.webRoot); 362 var re = new RegExp('^' + config.webRoot);
363 req.url = req.url.replace(re, ''); 363 var url = parseurl(req.url.replace(re, ''));
364 var ext = req.url.substring(req.url.lastIndexOf('.')+1); 364 var pathname = url.pathname || '';
365 var url = require('url').parse(req.url); 365 var ext = pathname.substring(pathname.lastIndexOf('.')+1);
366 366
367 if (url.pathname === '') 367 if (pathname === '')
368 return servePage(url, res); 368 return servePage(url, res);
369 369
370 fs.readFile(req.url, function(err, data) { 370 fs.readFile(pathname, function(err, data) {
371 if (err) 371 if (err || pathname === 'index.xhtml')
372 return servePage(url, res); 372 return servePage(url, res);
373 373
374 res.writeHead(200, {'Content-Type': config.types[ext] || 'application/octet-stream'}); 374 res.writeHead(200, {'Content-Type': config.types[ext] || 'application/octet-stream'});
375 375
376 res.end(data); 376 res.end(data);