Skip to content

Commit

Permalink
Tests as npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsxp committed Jul 11, 2014
1 parent 4e6fcd4 commit 2caebf3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 50 deletions.
21 changes: 21 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Get optional args
var args = process.argv.splice(2);
var formattedArgs = {};

// Simple args parse
for (var i = 0, l = args.length; i < l; i ++) {
if (args[i].match(/^-+/)) {
formattedArgs[args[i]] = args[i + 1];
}
};

// Setup constants
var ROOT_PATH = '';
var PORT = formattedArgs['--port'] || 3000;

if (formattedArgs['--root']) {
ROOT_PATH += formattedArgs['--root'] + '/';
}

var server = require('../server');
server.start(PORT, ROOT_PATH);
18 changes: 18 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var types = require('./types');

module.exports = {
getType: function (url) {
var type = url.split('.').pop();
// Default extension
if (type === '/') {
type = 'html';
}
return types[type];
},

notFound : function(url, res) {
console.log('Could find ', url);
res.writeHead(404, {"Content-Type": 'text/html'});
res.end();
}
}
9 changes: 9 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
'js' : 'application/javascript',
'json': 'application/json',
'html' : 'text/html',
'png': 'image/png',
'jpeg': 'image/jpeg',
'gif': 'image/gif'
};

17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.0.1",
"description": "Simple http server",
"main": "server.js",
"bin": {
"server" : "bin/cli.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
Expand All @@ -15,10 +18,20 @@
"HTTP",
"server"
],
"author": "Daniel Paulino",
"author": {
"name": "Daniel Paulino"
},
"license": "ISC",
"bugs": {
"url": "https://github.com/dpsxp/node-http-server/issues"
},
"homepage": "https://github.com/dpsxp/node-http-server"
"homepage": "https://github.com/dpsxp/node-http-server",
"readme": "node-http-server\n================\n\nNodeJS Simple HTTP Server\n\nThis a simple HTTP server made with nodejs. \nI know there's a lot of http server around, and also I should be using some task runner (because that's what cool kids are doing), But I made this one to better understand how nodejs work.\n\n## How to use\n\nFor now just copy it and run \n\n node server.js\n\nYou can also set optional params \n\n node server.js --root ROOT_PATH --port PORT\n\n",
"readmeFilename": "README.md",
"_id": "[email protected]",
"dist": {
"shasum": "d53b6de5a53ec61398e1875c12691670bab4edbf"
},
"_resolved": "git+https://github.com/dpsxp/node-http-server.git#4e6fcd42cea59ee293de2ea2337135aab35e11a5",
"_from": "git+https://github.com/dpsxp/node-http-server.git"
}
66 changes: 18 additions & 48 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
// Node simpe http server
// Get optional args
var args = process.argv.splice(2);
var formattedArgs = {};

// Required modules
var http = require('http');
var fs = require('fs');
var url = require('url');

// Simple args parse
for (var i = 0, l = args.length; i < l; i ++) {
if (args[i].match(/^-+/)) {
formattedArgs[args[i]] = args[i + 1];
}
};

// Setup constants
var helpers = require('./lib/helpers');
var ROOT_PATH = process.cwd() + '/';
var PORT = formattedArgs['--port'] || 3000;

if (formattedArgs['--root']) {
ROOT_PATH += formattedArgs['--root'] + '/';
}

var types = {
'js' : 'application/javascript',
'json': 'application/json',
'html' : 'text/html',
'png': 'image/png',
'jpeg': 'image/jpeg',
'gif': 'image/gif'
};

function getType (url) {
var type = url.split('.').pop();
// Default extension
if (type === '/') {
type = 'html';
}
return types[type];
}
var server = http.createServer();

function getFile (url) {
if (url === '/') {
Expand All @@ -50,28 +17,31 @@ function getFile (url) {
}
}

function notFound(url, res) {
console.log('Could find ', url);
res.writeHead(404, {"Content-Type": 'text/html'});
res.end();
}

var server = http.createServer();

server.on('request', function(req, res) {
var path = url.parse(req.url).pathname,
type,
file;

console.log('Serving request for ', path);
type = getType(path);
type = helpers.getType(path);
file = getFile(path);
console.log(path);

fs.createReadStream(file).on('error', function () {
notFound(path, res);
helpers.notFound(path, res);
}).pipe(res)
});

server.listen(PORT, function () {
console.log('Server started at port ' + PORT);
});
module.exports = {
start : function (port, root) {
var port = port || 3000;
if (root) {
ROOT_PATH += root + '/';
}

server.listen(port, function () {
console.log('Server started at port ' + port);
});
}
}

0 comments on commit 2caebf3

Please sign in to comment.