-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
158 lines (132 loc) · 3.77 KB
/
app.js
File metadata and controls
158 lines (132 loc) · 3.77 KB
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
var loopback = require('loopback');
var path = require('path');
var app = module.exports = loopback();
var started = new Date();
/*
// operational dependencies
try {
require('strong-agent').profile();
var control = require('strong-cluster-control');
var clusterOptions = control.loadOptions();
} catch(e) {
console.log('Could not load operational dependencies:');
console.log(e);
}
// if configured as a cluster master, just start controller
if(clusterOptions.clustered && clusterOptions.isMaster) {
return control.start(clusterOptions);
}
*/
/*
* 1. Configure LoopBack models and datasources
*
* Read more at http://apidocs.strongloop.com/loopback#appbootoptions
*/
app.boot(__dirname);
/*
* 2. Configure request preprocessing
*
* LoopBack support all express-compatible middleware.
*/
app.use(loopback.favicon());
app.use(loopback.logger(app.get('env') === 'development' ? 'dev' : 'default'));
app.use(loopback.cookieParser(app.get('cookieSecret')));
app.use(loopback.token({model: app.models.accessToken}));
app.use(loopback.bodyParser());
app.use(loopback.methodOverride());
/*
* EXTENSION POINT
* Add your custom request-preprocessing middleware here.
* Example:
* app.use(loopback.limit('5.5mb'))
*/
/*
* 3. Setup request handlers.
*/
// LoopBack REST interface
var apiPath = '/api';
app.use(apiPath, loopback.rest());
// API explorer (if present)
var explorerPath = '/explorer';
var explorerConfigured = false;
try {
var explorer = require('loopback-explorer');
app.use(explorerPath, explorer(app, { basePath: apiPath }));
explorerConfigured = true;
} catch(e){
// ignore errors, explorer stays disabled
}
/*
* EXTENSION POINT
* Add your custom request-handling middleware here.
* Example:
* app.use(function(req, resp, next) {
* if (req.url == '/status') {
* // send status response
* } else {
* next();
* }
* });
*/
// Let express routes handle requests that were not handled
// by any of the middleware registered above.
// This way LoopBack REST and API Explorer take precedence over
// express routes.
app.use(app.router);
// The static file server should come after all other routes
// Every request that goes through the static middleware hits
// the file system to check if a file exists.
app.use(loopback.static(path.join(__dirname, 'public')));
// Requests that get this far won't be handled
// by any middleware. Convert them into a 404 error
// that will be handled later down the chain.
app.use(loopback.urlNotFound());
/*
* 4. Setup error handling strategy
*/
/*
* EXTENSION POINT
* Add your custom error reporting middleware here
* Example:
* app.use(function(err, req, resp, next) {
* console.log(req.url, ' failed: ', err.stack);
* next(err);
* });
*/
// The ultimate error handler.
app.use(loopback.errorHandler());
/*
* 5. Add a basic application status route at the root `/`.
*
* (remove this to handle `/` on your own)
*/
app.get('/', loopback.status());
/*
* 6. Enable access control and token based authentication.
*/
var swaggerRemote = app.remotes().exports.swagger;
if (swaggerRemote) swaggerRemote.requireToken = false;
app.enableAuth();
/*
* 7. Optionally start the server
*
* (only if this module is the main module)
*/
app.start = function() {
return require('http').createServer(app).listen(app.get('port'), app.get('host'),
function(){
var baseUrl = 'http://' + app.get('host') + ':' + app.get('port');
if (explorerConfigured) {
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
} else {
console.log(
'Run `npm install loopback-explorer` to enable the LoopBack explorer'
);
}
console.log('LoopBack server listening @ %s%s', baseUrl, '/');
}
);
}
if(require.main === module) {
app.start();
}