-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (32 loc) · 875 Bytes
/
server.js
File metadata and controls
41 lines (32 loc) · 875 Bytes
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
//Server http feito em node
//Importa os módulos e cria o sistema com o request e o response
const http = require('http');
const server = http.createServer((req, res) =>{
//Informações do acesso ao servidor
const headers = req.headers;
const method = req.method;
const url = req.url;
/*
console.log("Headers");
console.log(headers);
console.log("Method: " + method);
console.log("URL: " + url);
*/
if(url == '/teste'){
res.setHeader('Content-type', 'text/html');
res.end('<h1>Olar mundos</h1>');
}
//Devolvendo Json
if(url == '/json'){
res.setHeader('Content-type', 'application/json');
const data = {
id: 1,
name: 'User'
};
res.end(JSON.stringify(data));
}
});
//Configura a porta a qual o http server irá responder
server.listen(3000, function(){
console.log('O servidor está online!');
});