-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (42 loc) · 1.35 KB
/
app.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
var express = require('express');
var path = require('path');
var log4js = require('log4js');
var cors = require('cors');
var indexRouter = require('./routes/index');
var weatherRouter = require('./routes/weather');
var apiRouter = require('./routes/api')
var app = express();
const {personalError} = require('./utils/errors');
const {checkAccess} = require('./utils/object');
const { crossOrigins } = require('./settings');
// view engine setup
const loggerLog4js = log4js.getLogger('http');
app.use(log4js.connectLogger(loggerLog4js, {level: 'auto', format: ':remote-addr - :method :url :status'}));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.options('*', cors({
origin: crossOrigins
})
);
app.use('/api', apiRouter)
app.use((req, res, next) => {
checkAccess(res, req.headers['api_key'], req.method, req.headers.origin).then(result => {
if(result == 0) {
next();
} else {
personalError(res, null, "NOT AUTHORIZED API KEY DOESN'T MATCH", 401);
}
}).catch(err => {
personalError(res, null, err, 403);
})
});
app.use('/', indexRouter);
app.use('/weather', weatherRouter)
app.use(function(req, res, next) {
personalError(res, null, "Generic not found", 404);
});
// error handler
app.use(function(err, req, res, next) {
personalError(res, err, "Unknown error", 500);
});
module.exports = app;