-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (41 loc) · 1.33 KB
/
index.js
File metadata and controls
49 lines (41 loc) · 1.33 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
import express from 'express'
import cors from 'cors'
import { HOST, PORT } from './config/config.js'
import apiRoutes from './routes/index.routes.js'
import path from 'path'
const app = express()
app.use(cors())
app.use(express.json())
// app.use(express.static('public'))
// app.use(express.static(path.join(__dirname, 'public')));
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "public")))
app.get('/', (req, res) => {
try {
res.setHeader("Content-Type", "text/html");
const landingHTML = `
<h1>Bienvenidos a nuestra REST-API</h1>
<p>Servidor iniciado en ${HOST}:${PORT}</p>
`;
res.status(200).send(landingHTML);
} catch (error) {
console.error('Error en el endpoint de inicio:', error);
res.status(500).send('Hubo un error en el servidor.');
}
});
app.use("/API/v1/", apiRoutes)
app.use((req, res, next) => {
const error = new Error('Ruta no encontrada');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
console.error('Error en el servidor:', error);
res.status(error.status || 500).json({
message: error.message || 'Ocurrió un error en el servidor',
status: error.status || 500
});
});
app.listen(PORT, () => {
console.log(`Iniciando API en ${HOST}:${PORT}`)
})