-
Notifications
You must be signed in to change notification settings - Fork 20
/
form.js
66 lines (61 loc) · 2.04 KB
/
form.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
var http = require('http'),
sys = require('sys'),
querystring = require('querystring');
http.createServer(function(request, response)
{
sys.puts('Request for ' + request.url);
switch (request.url)
{
case '/':
response.writeHead(200, { 'Content-Type' : 'text/html' });
response.write(
'<form action="/cadastrar" method="post">' +
'Nome: <input type="text" name="nome"><br />' +
'Email: <input type="text" name="email"><br />' +
'Telefone: <input type="text" name="telefone"><br />' +
'<input type="submit" value="Submit">' +
'</form>'
);
response.end();
break;
case '/cadastrar':
response.writeHead(200, { 'Content-Type' : 'text/html' });
post_handler(request, function(request_data){
response.write(
'JSON object:<br />' +
'<pre>' + sys.inspect(request_data) + '</pre>' +
'<hr>' +
'Dados:<br />' +
'Nome: <strong>' + request_data.nome + '</strong><br />' +
'Email: <strong>' + request_data.email + '</strong><br />'+
'Telefone: <strong>' + request_data.telefone + '</strong><br />'
);
response.end();
});
break;
default:
response.writeHead(400, { 'Content-Type' : 'text/html' });
response.write(
'Pagina nao encontrada!'
);
response.end();
};
}).listen(8001);
console.log("Servidor rodando na porta 8001");
function post_handler(request, callback)
{
var _REQUEST = { };
var _CONTENT = '';
if (request.method == 'POST')
{
request.addListener('data', function(chunk)
{
_CONTENT+= chunk;
});
request.addListener('end', function()
{
_REQUEST = querystring.parse(_CONTENT);
callback(_REQUEST);
});
};
};