Skip to content

Commit

Permalink
Split app and server
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Mar 16, 2015
1 parent 03740aa commit 29cc5dc
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 139 deletions.
138 changes: 138 additions & 0 deletions test/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
var fs = require('fs')
var url = require('url');
var querystring = require('querystring');

var routes = {
'/request': function(res, req) {
res.writeHead(200, {'Content-Type': 'application/json'});
var data = ''
req.on('data', function(c) { data += c })
req.on('end', function() {
res.end(JSON.stringify({
method: req.method,
url: req.url,
headers: req.headers,
data: data
}));
})
},
'/hello': function(res, req) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'X-Request-URL': 'http://' + req.headers.host + req.url
});
res.end('hi');
},
'/hello/utf8': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
// "hello"
var buf = new Buffer([104, 101, 108, 108, 111]);
res.end(buf);
},
'/hello/utf16le': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-16le'
});
// "hello"
var buf = new Buffer([104, 0, 101, 0, 108, 0, 108, 0, 111, 0]);
res.end(buf);
},
'/binary': function(res) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var buf = new Buffer(256);
for (var i = 0; i < 256; i++) {
buf[i] = i;
}
res.end(buf);
},
'/redirect/301': function(res) {
res.writeHead(301, {'Location': '/hello'});
res.end();
},
'/redirect/302': function(res) {
res.writeHead(302, {'Location': '/hello'});
res.end();
},
'/redirect/303': function(res) {
res.writeHead(303, {'Location': '/hello'});
res.end();
},
'/redirect/307': function(res) {
res.writeHead(307, {'Location': '/hello'});
res.end();
},
'/redirect/308': function(res) {
res.writeHead(308, {'Location': '/hello'});
res.end();
},
'/boom': function(res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('boom');
},
'/empty': function(res) {
res.writeHead(204);
res.end();
},
'/error': function(res) {
res.destroy();
},
'/form': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('number=1&space=one+two&empty=&encoded=a%2Bb&');
},
'/json': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({name: 'Hubot', login: 'hubot'}));
},
'/json-error': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('not json {');
},
'/cookie': function(res, req) {
var setCookie, cookie
var params = querystring.parse(url.parse(req.url).query);
if (params.name && params.value) {
setCookie = [params.name, params.value].join('=');
}
if (params.name) {
cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
}
res.writeHead(200, {'Content-Type': 'text/plain', 'Set-Cookie': setCookie});
res.end(cookie);
},
'/headers': function(res) {
res.writeHead(200, {
'Date': 'Mon, 13 Oct 2014 21:02:27 GMT',
'Content-Type': 'text/html; charset=utf-8'
});
res.end();
}
};

var types = {
js: 'application/javascript',
css: 'text/css',
html: 'text/html',
txt: 'text/plain'
};

module.exports = function(req, res) {
var pathname = url.parse(req.url).pathname;
var route = routes[pathname];
if (route) {
route(res, req);
} else {
fs.readFile(__dirname + '/..' + pathname, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': types.txt});
res.end('Not Found');
} else {
var ext = (pathname.match(/\.([^\/]+)$/) || [])[1]
res.writeHead(200, {'Content-Type': types[ext] || types.txt});
res.end(data);
}
});
}
};
142 changes: 3 additions & 139 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -1,143 +1,7 @@
#!/usr/bin/env node

var port = Number(process.argv[2] || 3000)

var fs = require('fs')
var http = require('http');
var url = require('url');
var querystring = require('querystring');

var routes = {
'/request': function(res, req) {
res.writeHead(200, {'Content-Type': 'application/json'});
var data = ''
req.on('data', function(c) { data += c })
req.on('end', function() {
res.end(JSON.stringify({
method: req.method,
url: req.url,
headers: req.headers,
data: data
}));
})
},
'/hello': function(res, req) {
res.writeHead(200, {
'Content-Type': 'text/plain',
'X-Request-URL': 'http://' + req.headers.host + req.url
});
res.end('hi');
},
'/hello/utf8': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
// "hello"
var buf = new Buffer([104, 101, 108, 108, 111]);
res.end(buf);
},
'/hello/utf16le': function(res) {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-16le'
});
// "hello"
var buf = new Buffer([104, 0, 101, 0, 108, 0, 108, 0, 111, 0]);
res.end(buf);
},
'/binary': function(res) {
res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var buf = new Buffer(256);
for (var i = 0; i < 256; i++) {
buf[i] = i;
}
res.end(buf);
},
'/redirect/301': function(res) {
res.writeHead(301, {'Location': '/hello'});
res.end();
},
'/redirect/302': function(res) {
res.writeHead(302, {'Location': '/hello'});
res.end();
},
'/redirect/303': function(res) {
res.writeHead(303, {'Location': '/hello'});
res.end();
},
'/redirect/307': function(res) {
res.writeHead(307, {'Location': '/hello'});
res.end();
},
'/redirect/308': function(res) {
res.writeHead(308, {'Location': '/hello'});
res.end();
},
'/boom': function(res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('boom');
},
'/empty': function(res) {
res.writeHead(204);
res.end();
},
'/error': function(res) {
res.destroy();
},
'/form': function(res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('number=1&space=one+two&empty=&encoded=a%2Bb&');
},
'/json': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({name: 'Hubot', login: 'hubot'}));
},
'/json-error': function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('not json {');
},
'/cookie': function(res, req) {
var setCookie, cookie
var params = querystring.parse(url.parse(req.url).query);
if (params.name && params.value) {
setCookie = [params.name, params.value].join('=');
}
if (params.name) {
cookie = querystring.parse(req.headers['cookie'], '; ')[params.name];
}
res.writeHead(200, {'Content-Type': 'text/plain', 'Set-Cookie': setCookie});
res.end(cookie);
},
'/headers': function(res) {
res.writeHead(200, {
'Date': 'Mon, 13 Oct 2014 21:02:27 GMT',
'Content-Type': 'text/html; charset=utf-8'
});
res.end();
}
};

var types = {
js: 'application/javascript',
css: 'text/css',
html: 'text/html',
txt: 'text/plain'
};
var app = require('./app');

http.createServer(function(req, res) {
var pathname = url.parse(req.url).pathname;
var route = routes[pathname];
if (route) {
route(res, req);
} else {
fs.readFile(__dirname + '/..' + pathname, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': types.txt});
res.end('Not Found');
} else {
var ext = (pathname.match(/\.([^\/]+)$/) || [])[1]
res.writeHead(200, {'Content-Type': types[ext] || types.txt});
res.end(data);
}
});
}
}).listen(port);
var port = parseInt(process.argv[2], 10) || 3000;
http.createServer(app).listen(port);

0 comments on commit 29cc5dc

Please sign in to comment.