-
Notifications
You must be signed in to change notification settings - Fork 1
/
app1.js
73 lines (63 loc) · 1.67 KB
/
app1.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var http = require('http');
var fs = require('fs');
var url = require("url");
var query = require("querystring"); //解析post
var ccap = require('ccap')();//Instantiated ccap class
var txt = '';
//生成验证码 start
var ary = ccap.get();
txt = ary[0];
var buf = ary[1];
console.log('txt');
console.log(txt);
//生成验证码 end
//将验证码存到本地 start
fs.writeFile("cap.jpeg", buf, function(err) {
if (err) {
console.log("errro!");
} else {
console.log("保存成功!");
}
});
//将验证码存到本地 end
http.createServer(function (request, response) {
if(request.url == '/cap.jpg'){
response.writeHead(200, {'Content-Type': 'image/jpg'});
response.end(buf);
}else if(request.url == '/r'){
if(request.method == 'POST'){
var postdata = "";
request.addListener("data",function(postchunk){
postdata += postchunk;
})
//POST结束输出结果
request.addListener("end",function(){
var params = query.parse(postdata);
if((params.code).toUpperCase() == txt){
response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
response.end('验证成功');
}else{
response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
response.end('验证错误');
}
})
}
}else{
if(request.method == 'GET'){
var body = '<html>'+
'<head>'+
'<meta charset="utf-8" />'+
'</head>'+
'<body>'+
'<form action="/r" method = "post">'+
'<input name="code" type="text" />'+
'<img src="cap.jpg" />'+
'<input type="submit" />'+
'</form>'
'</body>'+
'</html>';
response.end(body);
}
}
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');