-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo1-http.js
executable file
·51 lines (42 loc) · 1.38 KB
/
demo1-http.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
var http = require("http")
var fs = require("fs")
var path = require("path")
var express = require("express");
const querystring = require("querystring")
let urlObj = require("url");
var server = http.createServer()
server.listen(8080, function () {
console.log('正在运行中....')
})
server.on('request', function (request, response) {
let url = request.url;
let method = request.method;
// 第二个参数为 true,返回的对象当中会解析出 query 参数
const obj = urlObj.parse(url, true)
// console.log(obj)
// console.log(request.headers);s
console.log(url);
console.log(method);
if (method == 'GET' && obj.pathname == '/index'){
response.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
fs.readFile(path.join(__dirname, './index.html'),'utf-8', (err, data) =>{
if (err) return console.log('读取失败...');
response.end(data);
});
}else {
response.writeHead(404, {'content-type': 'text/plain;charset=utf-8', 'aaa':'dddddd'})
}
});
server.on('request', function (request, response) {
let str = ""
request.on('data', chunk =>{
str += chunk;
})
request.on('end', () =>{
console.log(str);
let obj = querystring.parse(str);
console.log(obj);
response.end('post')
});
});
console.log("Server running at port 8080")