-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhttp.js
More file actions
50 lines (45 loc) · 1.15 KB
/
http.js
File metadata and controls
50 lines (45 loc) · 1.15 KB
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
#! /usr/bin/env node
/*
* 使用前请先启动服务器,默认主机与端口为127.0.0.1:4445
* $ node bin/http.js
* 然后运行本Demo:
* $ node demo/http.js
*/
const http = require('http');
const HOST = '127.0.0.1';
const PORT = '4444';
// 发送HTTP请求
var req = http.request({
host: HOST,
port: PORT,
path: '/validate',
method: 'POST'
},
function(res) {
var result = '';
// 有数据到来时,追加到result字符串
res.on('data', x => result += x);
// 结束时,输出校验结果
res.on('end', function() {
console.log('normal validation:');
console.log(result);
});
});
req.end('<html></html>');
// 发送HTTP请求
req = http.request({
host: HOST,
port: PORT,
// 开启快速校验选项
path: '/validate?fast=true',
method: 'POST'
},
function(res) {
var result = '';
res.on('data', x => result += x);
res.on('end', function() {
console.log('fast validation:');
console.log(result);
});
});
req.end('<html></html>');