-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (38 loc) · 1.29 KB
/
server.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
const express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
helmet = require('helmet'),
addRequestId = require('express-request-id')(),
path = require('path'),
config = require('./config'),
routes = require('./routes/films'),
app = express();
//Logger
//@see: https://medium.com/@tobydigz/logging-in-a-node-express-app-with-morgan-and-bunyan-30d9bf2c07a
const loggerFormat = ':id [:date[web]] ":method :url" :status :response-time ms';
app.use(addRequestId);
morgan.token('id', req => req.id);
app.use(morgan(loggerFormat, {
skip: (req, res) => res.statusCode < 400,
stream: process.stderr
}));
app.use(morgan(loggerFormat, {
skip: (req, res) => res.statusCode >= 400,
stream: process.stdout
}));
//Middelware
app.use(helmet());
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json({ type: '*/*' }));
app.use(express.static('public'));
//Rutas
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
app.get('/api/films', routes.getAllFilms);
app.get('/api/films/:id', routes.getFilm);
app.post('/api/films', routes.createFilm);
app.put('/api/films', routes.updateFilm);
app.delete('/api/films', routes.destroyFilm);
// Puerto
app.listen(config.port);