Browse files

Move serve static file to router.

  • Loading branch information...
1 parent 2177863 commit 13af4e77f87fa7354679ae45fad2647f6cfa2a17 Maythee Anegboonlap committed Nov 16, 2011
Showing with 61 additions and 61 deletions.
  1. +59 −22 router.js
  2. +2 −39 server.js
View
81 router.js
@@ -1,11 +1,16 @@
-var log4js = require('log4js');
+var fs = require('fs'),
+ mime = require('mime'),
+ path = require('path'),
+ log4js = require('log4js'),
+ url = require('url');
var _log = log4js.getLogger('router');
-var Router = function(routes, store) {
+var Router = function(base, routes, store) {
var _self = this;
+ var _base = base;
var _store = store;
var _map = {
get: {},
@@ -40,33 +45,65 @@ var Router = function(routes, store) {
}
this.route = function route(request, response) {
- _log.debug ('route to: ' + request.method.toLowerCase() + ' - ' + request.url);
- var method = _map[request.method.toLowerCase()][request.url];
- if (method) {
- method(request, response, _store);
- } else {
+ var incoming = request.headers['x-forwarded-for'] ||
+ request.connection.remoteAddress;
+ _log.info ('(' + incoming + ') request: ' + request.url);
+ _log.debug ('route to: ' + request.method.toLowerCase() + ' - ' + request.url);
- var methods = _map[request.method.toLowerCase()];
- var match = null;
- for (var key in methods) {
-
- var pattern = new RegExp('^' + key + '.*$', 'i');
- _log.debug ('url: ' + request.url + ' pattern: ' + pattern);
- if (pattern.test(request.url)) {
- match = methods[key];
- break;
- }
-
+ var target = request.url == '/' ? '/index.html' : request.url;
+ var filePath = path.join(__dirname, _base, url.parse(target).pathname);
+
+ if (path.existsSync(filePath)) {
+ // Serve static file
+ var stat = fs.statSync(filePath);
+ if (stat.isDirectory()) {
+ _self.notfound(request, response);
+ } else {
+ response.writeHead(200, {
+ 'Content-Type': mime.lookup(filePath),
+ 'Content-Length': stat.size
+ });
+
+ var stream = fs.createReadStream(filePath);
+ stream.on('data', function (data) {
+ response.write(data);
+ });
+
+ stream.on('end', function() {
+ response.end();
+ });
}
- if (match) {
- match(request, response, _store);
+ } else {
+
+ var method = _map[request.method.toLowerCase()][request.url];
+ if (method) {
+ method(request, response, _store);
} else {
- _self.notfound(request, response);
- }
+ var methods = _map[request.method.toLowerCase()];
+ var match = null;
+ for (var key in methods) {
+
+ var pattern = new RegExp('^' + key + '.*$', 'i');
+ _log.debug ('url: ' + request.url + ' pattern: ' + pattern);
+ if (pattern.test(request.url)) {
+ match = methods[key];
+ break;
+ }
+
+ }
+
+ if (match) {
+ match(request, response, _store);
+ } else {
+ _self.notfound(request, response);
+ }
+
+ }
}
+
}
_parse(routes);
View
41 server.js
@@ -1,10 +1,8 @@
var fs = require('fs'),
http = require('http'),
log4js = require('log4js'),
- mime = require('mime'),
nowjs = require('now'),
path = require('path'),
- url = require('url'),
util = require('util');
// Parse configuration
@@ -45,45 +43,10 @@ var store = new Store(config.mongo);
// Initial router
var Router = require('./router.js').router;
-var router = new Router(config.routes, store);
+var router = new Router(config.base, config.routes, store);
// Initial server and now.js
-var httpServer = http.createServer(
- function(request, response) {
-
- var incoming = request.headers['x-forwarded-for'] ||
- request.connection.remoteAddress;
- console.log ('(' + incoming + ') request: ' + request.url);
-
- var target = request.url == '/' ? '/index.html' : request.url;
- var filePath = path.join(__dirname, config.base, url.parse(target).pathname);
-
- if (path.existsSync(filePath)) {
- // Serve static file
- var stat = fs.statSync(filePath);
- if (stat.isDirectory()) {
- router.notfound(request, response);
- } else {
- response.writeHead(200, {
- 'Content-Type': mime.lookup(filePath),
- 'Content-Length': stat.size
- });
-
- var stream = fs.createReadStream(filePath);
- stream.on('data', function (data) {
- response.write(data);
- });
-
- stream.on('end', function() {
- response.end();
- });
- }
-
- } else {
- router.route(request, response);
- }
-
- });
+var httpServer = http.createServer(router.route);
httpServer.listen(config.port);
console.info ('Listen to ' + config.port);

0 comments on commit 13af4e7

Please sign in to comment.