forked from able8/hello-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (33 loc) · 1.15 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
var http = require('http')
var url = require('url')
var querystring = require('querystring')
function startServer(route, handle) {
var onRequest = function (req, res) {
console.log('request received ' + req.url)
var pathname = url.parse(req.url).pathname
var data = ""
req.on("error", function (err) {
console.error(err)
}).on("data", function (chunk) {
data += chunk
}).on("end", function () {
if (req.mothod === "POST") {
if (data.length > 1e6) {
req.connection.destroy() // 如果数据很大,就断开
}
route(handle, pathname, res, querystring.parse(data))
} else {
var params = url.parse(req.url, true).query
route(handle, pathname, res, params)
}
})
// 或者
// var data = []
// data.push(chunk)
// data = Buffer.concat(data).toString()
}
var server = http.createServer(onRequest)
server.listen(3000)
console.log('server started on http://127.0.0.1:3000')
}
module.exports.startServer = startServer