-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (44 loc) · 1.69 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
'use strict';
var a127 = require('a127-magic');
var express = require('express');
var app = express();
module.exports = app; // for testing
var ip = process.env.IP || 'localhost';
var port = process.env.PORT || 10010;
// initialize a127 framework
a127.init(function(config) {
// include a127 middleware
app.use(a127.middleware(config));
// adding ui options
// install swagger ui https://github.com/apigee-127/magic/issues/6
if (process.env.NODE_ENV!='production') {
config['a127.magic'].swaggerObject.host = ip + ':' + port; //!!! this is set host for swagger.yaml
}
var swaggerTools = config['a127.magic'].swaggerTools;
app.use(swaggerTools.swaggerUi({
swaggerUi: config.ui.swaggerUi,
apiDocs: config.ui.apiDocs
}));
// error handler to emit errors as a json string
app.use(function(err, req, res, next) {
if (typeof err !== 'object') {
// If the object is not an Error, create a representation that appears to be
err = {
message: String(err) // Coerce to string
};
} else {
// Ensure that err.message is enumerable (It is not by default)
Object.defineProperty(err, 'message', { enumerable: true });
}
// Return a JSON representation of #/definitions/ErrorResponse
res.set('Content-Type', 'application/json');
res.end(JSON.stringify(err));
});
// begin listening for client requests
// app.listen(port, ip);
app.listen(port, function() {
console.log('Express server listening on port: ' + port);
return console.log('env = ' + app.get('env') + '\n__dirname = ' + __dirname + '\nprocess.cwd = ' + process.cwd());
});
console.log('try this:\ncurl http://' + ip + ':' + port + '/hello?name=Scott');
});