Skip to content

Commit 237d5bb

Browse files
committed
nodejs-httpserver init
1 parent 6badc26 commit 237d5bb

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const server = require("./server");
4+
const router = require("./router");
5+
const requestHandlers = require("./requestHandlers");
6+
7+
var handle = {};
8+
handle["/"] = requestHandlers.start;
9+
handle["/start"] = requestHandlers.start;
10+
handle["/upload"] = requestHandlers.upload;
11+
12+
server.start(router.route, handle);
13+

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "httpserver",
3+
"version": "1.0.0",
4+
"description": "implementation get&post with nodejs",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "LC",
10+
"license": "ISC"
11+
}

requestHandlers.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const querystring = require("querystring");
4+
const exec = require("child_process").exec;
5+
6+
function sleep(milliSecond){
7+
let startTime = new Date().getTime();
8+
while(new Date().getTime() < startTime + milliSecond);
9+
}
10+
11+
function start(response,postData){
12+
console.log("Request handler 'start' was called.");
13+
//sleep(10000);
14+
15+
var body = '<html>'+
16+
'<head>'+
17+
'<meta http-equiv="Content-Type" content="text/html; '+
18+
'charset=UTF-8" />'+
19+
'</head>'+
20+
'<body>'+
21+
'<form action="/upload" method="post">'+
22+
'<textarea name="text" rows="20" cols="60"></textarea>'+
23+
'<input type="submit" value="Submit text" />'+
24+
'</form>'+
25+
'</body>'+
26+
'</html>';
27+
28+
response.writeHead(200, {"Content-Type": "text/html"});
29+
response.write(body);
30+
response.end();
31+
32+
/* exec("dir *.*",{timeout:10000, maxBuffer:20000*1024},function(error,stdout,stderr){
33+
let content = "empty";
34+
response.writeHead(200, {"Content-Type":"text/plain;"});
35+
if(stdout !== ""){
36+
content = stdout;
37+
}
38+
else if(error !== ""){
39+
content = error.message;
40+
}
41+
else{
42+
content = "Can't use childprocess";
43+
console.log("stderr:",stderr);
44+
}
45+
response.write(content);
46+
response.end();
47+
}); */
48+
//return "Hello Start";
49+
}
50+
51+
function upload(response,postData){
52+
console.log("Request handler 'upload' was called.");
53+
response.writeHead(200, {"Content-Type": "text/plain"});
54+
//response.write("Hello Upload");
55+
response.write("You've sent:"+querystring.parse(postData).text);
56+
//response.write("You've sent:"+postData);
57+
response.end();
58+
}
59+
60+
exports.start = start;
61+
exports.upload = upload;

router.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
function route(handle,pathname,response,postData){
4+
console.log("About to route a request for " + pathname);
5+
6+
if(typeof handle[pathname] === 'function'){
7+
handle[pathname](response,postData);
8+
}
9+
else{
10+
console.log("No request handler found for " + pathname);
11+
response.writeHead(404,{"Content-Type":"text/plain"});
12+
response.write("404 Not found");
13+
response.end();
14+
}
15+
}
16+
17+
exports.route = route;

server.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const http = require("http");
4+
const url = require("url");
5+
6+
function start(route,handle){
7+
function onRequest(request, response){
8+
var postData = "";
9+
let pathname = url.parse(request.url).pathname;
10+
console.log("Request for %s received",pathname);
11+
12+
request.setEncoding("utf8");
13+
14+
request.addListener("data",function(postDataChunk){
15+
postData += postDataChunk;
16+
console.log("Received POST data chunk '%s'.",postDataChunk);
17+
});
18+
request.addListener("end",function(){
19+
console.log("postData:",postData);
20+
route(handle,pathname,response,postData);
21+
});
22+
23+
/* response.writeHead(200, {"Content-Type": "text/plain"});
24+
var content = route(handle,pathname);
25+
response.write(content);
26+
response.end(); */
27+
//route(handle,pathname,response);
28+
}
29+
http.createServer(onRequest).listen(8888);
30+
console.log("Server has started!");
31+
}
32+
33+
exports.start = start;

0 commit comments

Comments
 (0)