-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (36 loc) · 1.11 KB
/
app.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
const express = require('express'); //traes los paquetes de EXPRESS
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const app = express(); //llamas a la función express que te trae la aplicación
app.use(bodyParser.urlencoded({ extended: false}));
app.use(cookieParser());
//pug
app.set('view engine', 'pug');
//importar rutas de index.js
const Mainroutes= require('./routes');
const cardRoutes= require('./routes/cards');
app.use(Mainroutes);
app.use('/cards', cardRoutes);
//Middleware ej.
app.use((req, res, next)=>{
console.log("HEllo")
const err= new Error('Oh, noes!');
next(err);
});
app.use((req, res, next)=>{
console.log(req.message);
next();
});
//middleware for errors:
app.use((req, res, next) => {
const err= new Error('Not found');
err.status= 404;
next(err);
});
app.use((err, req, res, next)=>{
res.locals.error= err;
res.render('error');
});
app.listen(3000, ()=>{
console.log('The application is running on localhost:3000!')
}); //servidor y puerto 3000. También puede tener una función de callback como parametro