-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.38 KB
/
index.js
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
"use strict";
import express from 'express';
import dotenv from 'dotenv'
import cors from 'cors';
import { fileURLToPath } from 'url';
import { join, dirname } from 'path';
import { connectionDB } from "./database/mongodb.js"
import { Book } from "./database/models/book.js"
dotenv.config();
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT;
app.use(express.static('public'))
app.use(cors());
app.use(express.static(join(__dirname, "public")));
app.use(express.json());
app.set("views", join(__dirname, 'views'));
app.set("view engine", "ejs");
app.disable('x-powered-by');
// Conexion a la base de datos.
connectionDB();
// Ruta principal.
app.get('/', (req, res) => {
res.render("index");
});
// Ruta de los libros.
app.get('/book', (req, res) => {
res.render("book");
});
// Envia toda la info a un nuevo endpoint.
app.get('/book/api', async function (req, res) {
try {
const allCollection = await Book.find();
res.json(allCollection);
} catch (e) {
console.log(`El error es ${e}`);
res.status(500).json({ error: 'Error al obtener los datos.' });
}
});
app.use((req, res, next) => {
res.status(400).render("404", {
titulo: "404",
descripcion: "ERROR"
});
});
app.listen(PORT, () => {
console.log(`Server listening on port http://localhost:${PORT}`);
});