-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (52 loc) · 1.46 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
52
53
54
55
56
57
58
59
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var apiRoutes = require('./controllers/api');
var homeRoutes = require('./controllers/home');
var imagesRoutes = require('./controllers/images');
var usersRoutes = require('./controllers/users');
var config = require('./config');
var cookieParser = require('cookie-parser');
mongoose.connect(config.database,function(err){
if(err){
console.log("Error en la conexión con la base de datos");
}
else{
console.log("Conectado a mongo");
}
});
app.set('views', './views');
app.set('view engine', 'pug');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({
limit: '15mb',
extended: true
}))
app.use(bodyParser.json({
limit: '15mb',
extended: true
}));
app.use(cookieParser());
// use morgan to log requests to the console
app.use(morgan('dev'));
app.use('/api', apiRoutes);
app.use('/home', homeRoutes);
app.use('/images', imagesRoutes);
app.use('/users', usersRoutes);
//check if the user has cookie to redirect to admin panel
app.use(function (req, res, next) {
if('access_token' in req.cookies){
res.redirect('/home');
}
else{
next();
}
})
app.get('/', function(req, res) {
res.render('login');
})
var server = app.listen(3001, function() {
console.log('Server started on http://localhost:' + server.address().port)
})