@vagnerereno/exemplo-http 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.js +26 -0
- package/package.json +11 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Olá!!
|
package/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const http = require('http'); // Importa o módulo HTTP nativo
|
|
2
|
+
|
|
3
|
+
const server = http.createServer((req, res) => {
|
|
4
|
+
|
|
5
|
+
if (req.url === '/user') {
|
|
6
|
+
// Define o tipo de resposta como JSON
|
|
7
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
8
|
+
// Envia o JSON do "usuário"
|
|
9
|
+
|
|
10
|
+
const usuario = {
|
|
11
|
+
id: 1,
|
|
12
|
+
nome: "Maria",
|
|
13
|
+
email: "maria@teste.com"
|
|
14
|
+
};
|
|
15
|
+
res.end(JSON.stringify(usuario));
|
|
16
|
+
} else {
|
|
17
|
+
// Rota não encontrada
|
|
18
|
+
res.writeHead(404);
|
|
19
|
+
res.end("Rota não encontrada.");
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Servidor escutando na porta 3000
|
|
24
|
+
server.listen(3000, () => {
|
|
25
|
+
console.log("Servidor em execução em http://localhost:3000");
|
|
26
|
+
});
|