-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
128 lines (94 loc) · 3.99 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// =======================
// IMPORT DIPENDENZE
// =======================
var express = require('express');
var app = express();
var bodyParser = require('body-parser');// get POST parameteres
var morgan = require('morgan'); // automatic log of HTTP requests
var Q = require('q'); // Q promise
var mongoose = require('mongoose'); // models for Mongo
// Use q. Note that you **must** use `require('q').Promise`.
mongoose.Promise = require('q').Promise;
var config = require('./config'); // get our config file
// =======================
// SETUP GLOBAL LOGGER
// =======================
var winston = require('winston');
var fs = require('fs');
// create the log directory if it does not exist
if (!fs.existsSync(config['log-dir']))
{ fs.mkdirSync(config['log-dir']); }
var tsFormat = () => (new Date()).toLocaleDateString();
global.logger = new (winston.Logger)({
transports:[
new (winston.transports.Console)({
timestamp: tsFormat,
colorize: true,
level: config['ambiente-log']
/* qui setto il livello di debug da usare tra
{ error:0, warn:1, info:2, verbose:3, debug:4, silly:5 }
il più alto comprende anche i più bassi. Esempio:
se scelgo 'info', i log 'debug' verranno ignorati
*/
}),
new (winston.transports.File)({
filename: `${config['log-dir']}/errors.log`,
timestamp: tsFormat,
level: 'error'
/* solo per quanto riguarda gli errori,
oltre a scriverli sulla console
li salvo in un file errors.log
*/
})
]
});
// =======================
// INIZIALIZZAZIONE SERVER E PACCHETTI
// =======================
/*
if I make requests from a different domain of the server, I have to grant the access to that specific domain.
Example. I have deployed my server on http://myserver1.it
But I send requests from http://myserver2.it In this case I should enable 'myserver2.it' as granted domain.
in the rule 'Access-Control-Allow-Origin'. If I use '*', I enable any domain.
*/
app.use(function(req, res, next){
res.setHeader('Access-Control-Allow-Origin', '*'); //granted domains
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); //granted headers
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'); //granted http verbs
next();
});
var port = process.env.PORT || 8080; // used to create, sign, and verify tokens
mongoose.connect(config.database); // connect to database
// use body parser so we can get info from POST and/or URL parameters
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// use morgan to log requests to the console
app.use(morgan('dev')); // dev = development
// =======================
// ROUTES BASIC
// =======================
// basic route
app.get('/', function(req, res) {
res.send('Ciao! benvenuto nelle API GET del tutorial JWT su http://localhost:' + port);
});
app.post('/', function(req, res) {
res.send('Ciao! benvenuto nelle API POST del tutorial JWT su http://localhost:' + port);
});
// =======================
// ADMIN ROUTES
// =======================
var adminRoutes = require('./routes/admin/admin-index');
app.use('/admin', adminRoutes); // put /admin as prefix
// =======================
// API ROUTES
// =======================
var apiRoutes = require('./routes/api/api-index');
app.use('/api', apiRoutes); // put /admin as prefix
// =======================
// start the server ======
// =======================
app.listen(port);
logger.info('server avviato su localhost:' + port);
logger.info('#### trovi un client Angular di prova su https://github.com/De-Lac/AngularJs_client_example ####');
logger.debug('ambiente di log settato a debug');