-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (51 loc) · 1.81 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
/**
* @description: Main module for the app execution:
* - Declaration of express app
* - Definition of middlewares order of execution
* - Cors definition for allowed ip directions
* @required:
* - library: express - main lib to create the server
* - library: cors - it's to allow the connection of some ip's
* - object: config - it contains environment vars.
* - function: routerApi - it contains api's routes.
* - function:logError - middleware
* - function: errorHandler - middleware
* - function: ormErrorHandler - middleware
*/
const express = require('express');
const cors = require('cors');
const { config } = require('./config/config');
const routerApi = require('./routes');
const { logErrors,errorHandler,boomErrorHandler,ormErrorHandler } = require('./middlewares/error.handler');
const { checkApiKey } = require('./middlewares/auth.handler');
const { mqttConnect,mqttSubscribe } = require('./mqtt/mqtt.subscribe');
const subsTopic = "/nodejs/mqtt"
const app = express();
app.use(express.json()); //it converts any requets in a json-format request.
const whitelist = ['http://localhost:8080', 'https://localhost:8083','http://localhost:3000'];
const options = {
origin: (origin, callback) => {
if (whitelist.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error('Request is not allowed!'));
}
}
}
app.use(cors(options));
require('./utils/auth'); //Aqui habilitamos la estrategia local para autenticacion
routerApi(app);
app.use(logErrors);
app.use(ormErrorHandler);
app.use(boomErrorHandler);
app.use(errorHandler);
app.get('/',
checkApiKey,
(req, res) => {
res.send('Server from express!');
});
app.listen(config.appPort,()=>{
console.log(`App listening to port ${config.appPort}`);
});
//const client = mqttConnect();
//mqttSubscribe(client,subsTopic);