-
Notifications
You must be signed in to change notification settings - Fork 0
/
routers.js
92 lines (74 loc) · 2.35 KB
/
routers.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
'use strict';
var express = require('express'),
router = express.Router();
var dataLoader = require('./data-loader.js');
var parser = require('./data-parser.js');
const MOTORWAYS = require('./data.js');
const config = require('./config/main.config');
var log = require('bunyan').createLogger({
name: 'accesslog',
streams: [{
level: config.logLevel,
path: config.accesslogPath
}]
});
// Accesslog middleware
// This middleware will log every possibie client ips using bunyan module
router.use(function(req, res, next){
if(req.path === '/favicon.ico'){ // ignore the request for favicon.ico
return next();
}
log.info({
path: req.url,
xReadIp: req.headers['x-real-ip'],
xForwardFor: req.headers['x-forwarded-for'],
remoteAddress: req.connection.remoteAddress,
socketRemoteAddress: req.socket.remoteAddress
}, 'Access log');
next();
});
router.get(['/', '/readme'], function(req, res, next){
res.redirect(302, '/readme.html');
});
router.get(['/info'], function(req, res, next){
res.json({message: 'This is RTSM API, a realtime traffic data of motorways in Taiwan by Roy Lu. July 2017'});
});
/**
* Main endpoint
*/
router.get('/data/:mid', async function(req, res, next){
try{
const result = {};
const mid = req.params.mid;
const motorway = MOTORWAYS[mid];
if(!motorway){
return res.json({message: 'Invalid motorway id'});
}
const nameTw = motorway.nameTw;
const nameEn = motorway.nameEn;
const url = motorway.url;
const rawData = await dataLoader(url);
const parsedData = await parser(rawData); // parser() returns an array
result.name = nameTw;
result.nameEn = nameEn;
result.direction = motorway.direction;
result.traffic = parsedData;
result.updatedAt = new Date().toISOString();
return res.json(result);
}catch(ex){
log.error({ex: ex.stack}, 'Error in get>/data/:mid');
return res.sendStatus(404);
}
});
// handle 404 issues
router.use(function(req, res, next){
//res.sendStatus(404);
res.send('What do you look for?');
});
router.use(function(err, req, res, next){
log.error('Error happened in routers');
log.error('Error: ', err);
res.send("很抱歉,暫時無法提供此服務,請稍後再試。"
+ "The service is currently not available. Please try it later.");
});
module.exports = router;