-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
54 lines (38 loc) · 1.26 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var http = require('http');
var fs = require('fs');
var url = require('url');
var logger = require('./logger');
var newlineTrans = require('./newlineTransformer');
var markdownTrans = require('./markdownTransformer');
var limitTrans = require('./limitTransformer');
var handleRequest = function(request, response){
var purl = url.parse(request.url, true);
if(request.url === '/favicon.ico'){
response.writeHead(200);
response.end();
return;
} else if(purl.pathname === '/lipsum'){
response.writeHead(200, {'Content-Type': 'text/html'});
var file = fs.createReadStream('lipsum.txt');
var limit = purl.query.limit;
file.pipe(limitTrans(limit)).pipe(newlineTrans()).pipe(response);
file.on("finished", function(){
response.end();
});
} else if(request.url === '/markdown'){
response.writeHead(200, {'Content-Type': 'text/html'});
var file = fs.createReadStream('sample.md');
file.pipe(markdownTrans()).pipe(response);
file.on("finished", function(){
response.end();
});
} else {
response.writeHead(404);
response.end("404 Not Found");
}
logger(request, response);
}
var server = http.createServer(handleRequest);
server.listen(7000, function(){
console.log("Listening on port 7000");
});