-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
39 lines (29 loc) · 1022 Bytes
/
Copy pathapp.js
File metadata and controls
39 lines (29 loc) · 1022 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
const express = require('express');
const path = require('path');
const produtosRoutes = require('./routes/produtosRoutes');
const notFound = require('./middleware/notFound');
const errorHandler = require('./middleware/errorHandler');
// Inicializa o app Express
const app = express();
// Middleware para processar JSON
app.use(express.json());
// Middleware para servir arquivos estáticos
app.use(express.static(path.join(__dirname, 'public')));
// Rotas da API
app.use('/api/produtos', produtosRoutes);
// Rota principal - serve o arquivo index.html
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Middleware para rotas não encontradas
app.use(notFound);
// Middleware para tratamento de erros
app.use(errorHandler);
// Porta do servidor
const PORT = process.env.PORT || 3000;
// Inicia o servidor
app.listen(PORT, () => {
console.log(`Servidor rodando na porta ${PORT}`);
console.log(`Acesse: http://localhost:${PORT}`);
});
module.exports = app;