-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (46 loc) · 1.67 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
// server.js
console.log('loading server...');
// configurando todas las herramientas necesarias
var express = require('express'); // libreria encargada de gestionar peticiones http
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose'); // libreria encargada de conecar con la base de datos MongoDB
var bodyParser = require('body-parser'); // libreria para recibir json
var cors = require('cors');
var fs = require('fs');
var log4js = require('log4js');
log4js.configure('config/log4js.json', {});
var expressLogger = log4js.getLogger('express');
var mainLogger = log4js.getLogger('main');
var errorLogger = log4js.getLogger('error');
if(process.env.NODE_ENV === 'test') {
// log4js.clearAppenders();
}
// configurando bd
var configDB = require('./config/database.js');
mongoose.connect(configDB.url, function(err) {
if (err) {
errorLogger.error('[Could not connect to MongoDB!] | ' +
'[Error: ' + err + ']');
process.exit(1);
}
});
var connect = require('connect');
// configurando herramientas de la libreria express
// if(process.env.NODE_ENV != 'test') app.use(morgan('dev')); // registra cada peticion a la consola
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(cors());
app.use(log4js.connectLogger(expressLogger, { level: 'auto' }));
// configurando modelos
require('./app/models/song');
require('./app/models/user');
// configurando direcciones
require('./app/routes.js')(app);
require('./config/updatedatabase.js')(fs);
// lanzando servidor
app.listen(port);
// Expose app
exports = module.exports = app;
mainLogger.trace('MoonSongs is up, listening on port: ' + port);