Skip to content

Commit 2caebf3

Browse files
committed
Tests as npm package
1 parent 4e6fcd4 commit 2caebf3

File tree

5 files changed

+81
-50
lines changed

5 files changed

+81
-50
lines changed

bin/cli.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Get optional args
2+
var args = process.argv.splice(2);
3+
var formattedArgs = {};
4+
5+
// Simple args parse
6+
for (var i = 0, l = args.length; i < l; i ++) {
7+
if (args[i].match(/^-+/)) {
8+
formattedArgs[args[i]] = args[i + 1];
9+
}
10+
};
11+
12+
// Setup constants
13+
var ROOT_PATH = '';
14+
var PORT = formattedArgs['--port'] || 3000;
15+
16+
if (formattedArgs['--root']) {
17+
ROOT_PATH += formattedArgs['--root'] + '/';
18+
}
19+
20+
var server = require('../server');
21+
server.start(PORT, ROOT_PATH);

lib/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var types = require('./types');
2+
3+
module.exports = {
4+
getType: function (url) {
5+
var type = url.split('.').pop();
6+
// Default extension
7+
if (type === '/') {
8+
type = 'html';
9+
}
10+
return types[type];
11+
},
12+
13+
notFound : function(url, res) {
14+
console.log('Could find ', url);
15+
res.writeHead(404, {"Content-Type": 'text/html'});
16+
res.end();
17+
}
18+
}

lib/types.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
'js' : 'application/javascript',
3+
'json': 'application/json',
4+
'html' : 'text/html',
5+
'png': 'image/png',
6+
'jpeg': 'image/jpeg',
7+
'gif': 'image/gif'
8+
};
9+

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.0.1",
44
"description": "Simple http server",
55
"main": "server.js",
6+
"bin": {
7+
"server" : "bin/cli.js"
8+
},
69
"scripts": {
710
"test": "echo \"Error: no test specified\" && exit 1",
811
"start": "node server.js"
@@ -15,10 +18,20 @@
1518
"HTTP",
1619
"server"
1720
],
18-
"author": "Daniel Paulino",
21+
"author": {
22+
"name": "Daniel Paulino"
23+
},
1924
"license": "ISC",
2025
"bugs": {
2126
"url": "https://github.com/dpsxp/node-http-server/issues"
2227
},
23-
"homepage": "https://github.com/dpsxp/node-http-server"
28+
"homepage": "https://github.com/dpsxp/node-http-server",
29+
"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",
30+
"readmeFilename": "README.md",
31+
32+
"dist": {
33+
"shasum": "d53b6de5a53ec61398e1875c12691670bab4edbf"
34+
},
35+
"_resolved": "git+https://github.com/dpsxp/node-http-server.git#4e6fcd42cea59ee293de2ea2337135aab35e11a5",
36+
"_from": "git+https://github.com/dpsxp/node-http-server.git"
2437
}

server.js

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
// Node simpe http server
2-
// Get optional args
3-
var args = process.argv.splice(2);
4-
var formattedArgs = {};
5-
62
// Required modules
73
var http = require('http');
84
var fs = require('fs');
95
var url = require('url');
10-
11-
// Simple args parse
12-
for (var i = 0, l = args.length; i < l; i ++) {
13-
if (args[i].match(/^-+/)) {
14-
formattedArgs[args[i]] = args[i + 1];
15-
}
16-
};
17-
18-
// Setup constants
6+
var helpers = require('./lib/helpers');
197
var ROOT_PATH = process.cwd() + '/';
20-
var PORT = formattedArgs['--port'] || 3000;
218

22-
if (formattedArgs['--root']) {
23-
ROOT_PATH += formattedArgs['--root'] + '/';
24-
}
25-
26-
var types = {
27-
'js' : 'application/javascript',
28-
'json': 'application/json',
29-
'html' : 'text/html',
30-
'png': 'image/png',
31-
'jpeg': 'image/jpeg',
32-
'gif': 'image/gif'
33-
};
34-
35-
function getType (url) {
36-
var type = url.split('.').pop();
37-
// Default extension
38-
if (type === '/') {
39-
type = 'html';
40-
}
41-
return types[type];
42-
}
9+
var server = http.createServer();
4310

4411
function getFile (url) {
4512
if (url === '/') {
@@ -50,28 +17,31 @@ function getFile (url) {
5017
}
5118
}
5219

53-
function notFound(url, res) {
54-
console.log('Could find ', url);
55-
res.writeHead(404, {"Content-Type": 'text/html'});
56-
res.end();
57-
}
58-
59-
var server = http.createServer();
60-
6120
server.on('request', function(req, res) {
6221
var path = url.parse(req.url).pathname,
6322
type,
6423
file;
6524

6625
console.log('Serving request for ', path);
67-
type = getType(path);
26+
type = helpers.getType(path);
6827
file = getFile(path);
28+
console.log(path);
6929

7030
fs.createReadStream(file).on('error', function () {
71-
notFound(path, res);
31+
helpers.notFound(path, res);
7232
}).pipe(res)
7333
});
7434

75-
server.listen(PORT, function () {
76-
console.log('Server started at port ' + PORT);
77-
});
35+
module.exports = {
36+
start : function (port, root) {
37+
var port = port || 3000;
38+
if (root) {
39+
ROOT_PATH += root + '/';
40+
}
41+
42+
server.listen(port, function () {
43+
console.log('Server started at port ' + port);
44+
});
45+
}
46+
}
47+

0 commit comments

Comments
 (0)