This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
122 lines (113 loc) · 2.92 KB
/
example.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var pkg = {
auditor: require('../'),
formidable: require('formidable'),
http: require('http'),
log: require('util').log
},
RULES = {
txt: {
type: 'text',
required: true,
max: 40
},
txt_multi: {
type: 'text',
required: false,
multiline: true,
min: 1,
max: 40
},
signed: {
type: 'signed',
required: true,
min: -10,
max: 100
},
unsigned: {
type: 'unsigned',
required: true,
min: 1,
max: 100
},
email: {
type: 'email',
required: true
},
email_loose: {
type: 'email_loose',
required: true
},
url: {
type: 'url',
required: true
},
date: {
type: 'date',
required: true,
func: function(val){
return new Date(val);
}
}
};
function Inspect( obj, level ){
return require('util').inspect( obj, true, level );
}
function App()
{
var label = 'test',
auditor = new pkg.auditor.audit(),
finish = function( res )
{
res.writeHead( 200, {
'Content-Type': 'text/plain'
});
res.end( Inspect( data ) );
},
data = {
errno:0,
fields: {}
};
auditor.add( label, RULES );
pkg.http.createServer(function(req,res)
{
if( req.method === 'POST' )
{
var form = new pkg.formidable.IncomingForm(),
fields = auditor.getFields( label, true );
form.on( 'field', function( field, val )
{
delete fields[field];
if( ( val = auditor.check( label, field, val ) ) ){
data.fields[field] = val;
data.errno += val.errno;
}
})
.on( 'file', function( field, val ){
data.files[field] = val;
})
.on( 'error', function( err ){
console.log( err );
finish(res);
})
.on( 'end', function()
{
var val = undefined;
for( var p in fields )
{
if( ( val = auditor.check( label, p, undefined ) ) ){
data.fields[p] = val;
data.errno += val.errno;
}
}
finish(res);
});
form.parse(req);
}
else {
finish(res);
}
}).listen( 1080, "127.0.0.1", function(){
pkg.log('Server running at http://127.0.0.1:1080/');
});
}
new App();