-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·129 lines (105 loc) · 3.2 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
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
import express from 'express';
import path from 'path';
import favicon from 'serve-favicon';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import debug from 'debug';
import http from 'http';
import logger from './logger';
import fs from 'fs';
import proxy from 'express-http-proxy';
import stream from 'stream';
import { NODE_ENV, PORT, COUCHDB_URL, UPLOAD_LIMIT } from './env';
import indexRouter from './routes/index';
import apiRouter from './routes/api';
import { registerCytoscapeExtensions } from '../model/cy-extensions';
import { secrets } from './secrets';
import PouchDB from 'pouchdb';
import PouchDBMemoryAdapter from 'pouchdb-adapter-memory';
// extensions/plugins
PouchDB.plugin(PouchDBMemoryAdapter);
registerCytoscapeExtensions();
const debugLog = debug('cytoscape-home:server');
const app = express();
const server = http.createServer(app);
// view engine setup
app.set('views', path.join(__dirname, '../', 'views'));
// define an inexpensive html engine that doesn't do serverside templating
app.engine('html', function (filePath, options, callback){
fs.readFile(filePath, function (err, content) {
if( err ){ return callback( err ); }
return callback( null, content.toString() );
});
});
app.set('view engine', 'html');
app.use(favicon(path.join(__dirname, '../..', 'public', 'icon.png')));
app.use(morgan('dev', {
stream: new stream.Writable({
write( chunk, encoding, next ){
logger.info( chunk.toString('utf8').trim() );
next();
}
})
}));
app.use(bodyParser.json({ limit: UPLOAD_LIMIT }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../..', 'public')));
// proxy requests under /db to the CouchDB server
app.use('/db', secrets); // apply security before proxy
app.use('/db', proxy(COUCHDB_URL));
app.use('/api', apiRouter);
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function(req, res) {
res.render('index.html');
});
// development error handler
// will print stacktrace
if (NODE_ENV === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error.html');
});
}
// production error handler
// no stacktraces leaked to user
// error page handler
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error.html');
});
app.set('port', PORT);
server.listen(PORT);
server.on('error', onError);
server.on('listening', onListening);
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
let bind = typeof PORT === 'string'
? 'Pipe ' + PORT
: 'Port ' + PORT;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
logger.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
logger.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debugLog('Listening on ' + bind);
}
export default app;