-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (39 loc) · 1.03 KB
/
index.js
File metadata and controls
50 lines (39 loc) · 1.03 KB
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
const express = require('express');
const routerApi = require('./routes');
const cors = require('cors');
const { checkApiKey } = require('./middlewares/auth.handler');
const {
logErrors,
errorHandler,
boomErrorHandler,
ormErrorHandler,
} = require('./middlewares/error.handler');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
const whitelist = ['http://localhost:3000, http://localhost:8000'];
const options = {
origin: (origin, callback) => {
if (whitelist.includes(origin) || !origin) {
callback(null, true);
} else {
callback(new Error('No permitido'));
}
},
};
app.use(cors(options));
require('./utils/auth');
app.get('/', (req, res) => {
res.send('Random products API');
});
app.get('/nuevaruta', checkApiKey, (req, res) => {
res.send('Testing API key auth');
});
routerApi(app);
app.use(logErrors);
app.use(ormErrorHandler);
app.use(boomErrorHandler);
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Server running at port ${PORT}`);
});