-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (150 loc) · 5.19 KB
/
server.js
File metadata and controls
164 lines (150 loc) · 5.19 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
// Função para ler arquivos de forma assíncrona
function lerArquivo(caminhoArquivo) {
return new Promise((resolve, reject) => {
fs.readFile(caminhoArquivo, 'utf8', (err, conteudo) => {
if (err) {
reject(err);
return;
}
resolve(conteudo);
});
});
}
// Função para listar arquivos em um diretório
function listarArquivos(diretorio) {
return new Promise((resolve, reject) => {
fs.readdir(diretorio, (err, arquivos) => {
if (err) {
reject(err);
return;
}
resolve(arquivos);
});
});
}
const server = http.createServer(async (req, res) => {
const { url } = req;
// Definir cabeçalho para resposta HTML
res.setHeader('Content-Type', 'text/html; charset=utf-8');
try {
// Roteamento
if (url === '/' || url === '/home') {
const conteudo = await lerArquivo(path.join(__dirname, 'public', 'index.html'));
res.statusCode = 200;
res.end(conteudo);
}
else if (url === '/sobre') {
const conteudo = await lerArquivo(path.join(__dirname, 'public', 'sobre.html'));
res.statusCode = 200;
res.end(conteudo);
}
else if (url === '/contato') {
const conteudo = await lerArquivo(path.join(__dirname, 'public', 'contato.html'));
res.statusCode = 200;
res.end(conteudo);
}
else if (url === '/arquivos') {
// Lista os arquivos da pasta 'public'
const arquivos = await listarArquivos(path.join(__dirname, 'public'));
let html = `
<html>
<head>
<title>Lista de Arquivos</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }
h1 { color: #333; }
ul { list-style-type: none; padding: 0; }
li { padding: 8px; margin-bottom: 5px; background-color: #f5f5f5; border-radius: 4px; }
a { color: #0066cc; text-decoration: none; }
a:hover { text-decoration: underline; }
.menu { margin-bottom: 20px; padding: 10px; background-color: #333; }
.menu a { color: white; margin-right: 15px; }
</style>
</head>
<body>
<div class="menu">
<a href="/">Home</a>
<a href="/sobre">Sobre</a>
<a href="/contato">Contato</a>
<a href="/arquivos">Arquivos</a>
</div>
<h1>Lista de Arquivos do Diretório Public</h1>
<ul>
`;
arquivos.forEach(arquivo => {
html += `<li>${arquivo}</li>`;
});
html += `
</ul>
</body>
</html>
`;
res.statusCode = 200;
res.end(html);
}
else {
// Tenta servir arquivo estático se existir
try {
const filePath = path.join(__dirname, 'public', url);
const conteudo = await lerArquivo(filePath);
// Identifica o tipo de conteúdo baseado na extensão do arquivo
const ext = path.extname(url);
let contentType = 'text/html';
switch (ext) {
case '.css':
contentType = 'text/css';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
case '.jpeg':
contentType = 'image/jpeg';
break;
}
res.setHeader('Content-Type', contentType);
res.statusCode = 200;
res.end(conteudo);
} catch (erro) {
// Se não encontrar o arquivo, retorna 404
res.statusCode = 404;
const conteudo = await lerArquivo(path.join(__dirname, 'public', '404.html'));
res.end(conteudo);
}
}
} catch (erro) {
console.error('Erro:', erro);
res.statusCode = 500;
res.end(`
<html>
<head>
<title>Erro Interno</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
h1 { color: #d9534f; }
p { color: #666; }
</style>
</head>
<body>
<h1>500 - Erro Interno do Servidor</h1>
<p>Ocorreu um erro ao processar sua solicitação.</p>
<p><a href="/">Voltar para a página inicial</a></p>
</body>
</html>
`);
}
});
// Inicializa o servidor
server.listen(PORT, () => {
console.log(`Servidor rodando em http://localhost:${PORT}`);
});