-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
188 lines (134 loc) · 4.74 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
__ ___ _____ _ _____ __
/\ \ \/ __\ \_ \ /_\ /__ \/ _\
/ \/ / / / /\/ //_\\ / /\/\ \
/ /\ / /___/\/ /_ / _ \/ / _\ \
\_\ \/\____/\____/ \_/ \_/\/ \__/
*/
// **********************************************************
// app.js
console.log('NCI ATS - Agile BPA - Food Recall Impact App');
// **********************************************************
// require
var http = require("http");
var https = require("https");
var url = require('url');
var express = require('express');
var js2xmlparser = require('js2xmlparser');
var path = require('path');
var fsr = require('file-stream-rotator');
var fs = require('fs');
var morgan = require('morgan');
//var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var server_api = require("./server/api.js");
//var config_json = require('./config/config.json');
var package_json = require('./package.json');
// **********************************************************
// config
//var config_port = config_json.app_port || process.env.NODE_APP_PORT;
var config_app_port = process.env.NODE_APP_PORT;
var config_fda_url = process.env.NODE_FDA_URL;
var config_fda_key = process.env.NODE_FDA_KEY;
var config_db_url = process.env.NODE_DB_URL;
// **********************************************************
// console start
console.log('package_json.name : '+ package_json.name );
console.log('package_json.version : '+ package_json.version );
console.log('package_json.description : '+ package_json.description );
console.log('config_app_port : '+ config_app_port );
console.log('config_fda_url : '+ config_fda_url );
console.log('config_fda_key : '+ config_fda_key );
console.log('config_db_url : '+ config_db_url );
/*
console.log('config_json.host : '+ config_json.app_host );
console.log('config_json.port : '+ config_json.app_port );
console.log('config_json.fda_host : '+ config_json.fda_host );
console.log('config_json.fda_key : '+ config_json.fda_key );
console.log('config_json.db_url : '+ config_json.db_url );
*/
// **********************************************************
// log using morgan
var logDirectory = __dirname + '/log';
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
var accessLogStream = fsr.getStream({
filename: logDirectory + '/search-api-%DATE%.log',
frequency: 'daily',
verbose: false
});
var app = express();
app.use(morgan('combined', {stream: accessLogStream}))
// **********************************************************
//app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// **********************************************************
// route
app.use('/', express.static(__dirname + '/client'));
app.get('/api', function(req, res){
server_api.requestSearch(req,res);
});
app.get('/api/search', function(req, res){
server_api.requestSearch(req,res);
});
app.get('/api/search.:ext', function(req, res){
server_api.requestSearch(req,res);
});
app.get('/api/count', function(req, res){
server_api.requestCounts(req,res);
});
app.get('/api/count.:ext', function(req, res){
server_api.requestCounts(req,res);
});
app.get('/api/crowd', function(req, res){
server_api.requestCrowd(req,res);
});
app.get('/api/crowd.:ext', function(req, res){
server_api.requestCrowd(req,res);
});
app.post('/api/crowd', function(req, res){
server_api.insertCrowd(req,res);
});
app.post('/api/crowd.:ext', function(req, res){
server_api.insertCrowd(req,res);
});
// **********************************************************
// error
app.use(function(req, res) {
var err_res = {};
err_res.responseStatus = {
'status': 404,
'type': 'Not Found',
'err': req.url +' Not Found'
};
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.status(404)
.send(err_res);
});
app.use(function(err, req, res, next) {
console.log('\n\n app.use error: ' + err );
console.error(err.stack);
var err_res = {};
err_res.responseStatus = {
'status': 500,
'type': 'Internal Server Error',
'err': err.name +': '+ err.message
};
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.status(500)
.send(err_res);
});
process.on('uncaughtException', function (err) {
console.log('\n\n uncaughtException: '+ err);
console.error(err.stack);
});
// **********************************************************
// server
var server = app.listen(config_app_port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('\n listening at http://%s:%s', host, port);
});
module.exports = app;