-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (42 loc) · 1.63 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
50
51
'use strict'
//Cargar modulos de NODEjs para crear el servidor
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var env = process.env.NODE_ENV
//Ejecutar express para trbajar con http
var app = express();
//Cargar ficheros rutas
var article_routes = require('./routes/article');
var video_routes = require('./routes/video');
//Cargar middlewares
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//Activar el cors
// Configurar cabeceras y cors
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
//Añadir prefijos a rutas
app.use('/', article_routes);
app.use('/', video_routes);
mongoose.set('useFindAndModify', false);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true, useUnifiedTopology: true})
.then(() => {
console.log('La conexion a la base de datos se ha realizado bien');
//Crear servidor y escuchar peiticones
});
if (env === 'production') {
app.get('/', express.static(`${__dirname}/fronted/build`))
}
var port = process.env.PORT || 3100;
app.listen(port, function () {
console.log(`App running on port ${port}`);
});
//Exportar el modulo
module.exports = app;