1+ // var loopback = require('loopback');
2+ // var boot = require('loopback-boot');
3+ // var path = require('path');
4+ //
5+ // var app = module.exports = loopback();
6+ // var http = require('http');
7+ // var https = require('https');
8+ // var sslCert = require('./private/ssl_cert');
9+ // var httpsOptions = {
10+ // key: sslCert.privateKey,
11+ // cert: sslCert.certificate
12+ // };
13+ // app.httpsOptions = httpsOptions;
14+ // // Bootstrap the application, configure models, datasources and middleware.
15+ // // Sub-apps like REST API are mounted via boot scripts.
16+ // boot(app, __dirname, function (err) {
17+ // if (err) throw err;
18+ // });
19+ // app.start = function (httpOnly) {
20+ // // start the web server
21+ // // return app.listen(function () {
22+ // // app.emit('started');
23+ // // var baseUrl = app.get('url').replace(/\/$/, '');
24+ // // console.log('Web server listening at: %s', baseUrl);
25+ // // if (app.get('loopback-component-explorer')) {
26+ // // var explorerPath = app.get('loopback-component-explorer').mountPath;
27+ // // console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
28+ // // }
29+ // // });
30+ //
31+ // //https 方式启动
32+ // var port = app.get('port');
33+ // console.log(port)
34+ // var host = app.get('host');
35+ // var httpServer = http.createServer(app).listen(port, host, function () {
36+ //
37+ // var httpsPort = app.get('https-port');
38+ // var httpsServer = https.createServer(httpsOptions, app).listen(httpsPort,
39+ // host, function () {
40+ //
41+ // app.emit('started');
42+ //
43+ // app.close = function (cb) {
44+ // app.removeAllListeners('started');
45+ // app.removeAllListeners('loaded');
46+ // httpServer.close(function () {
47+ // httpsServer.close(cb);
48+ // });
49+ // };
50+ // });
51+ // });
52+ //
53+ //
54+ //
55+ // };
56+ //
57+ // // start the server if `$ node server.js`
58+ // if (require.main === module) {
59+ // app.start();
60+ // }
61+ // //var staticPath = null;
62+ // //
63+ // //staticPath = path.resolve(__dirname, '../client/');
64+ // //console.log("Running app in development mode");
65+ // //
66+ // //app.use(loopback.static(staticPath));
67+ //
68+ // // Bootstrap the application, configure models, datasources and middleware.
69+ // // Sub-apps like REST API are mounted via boot scripts.
70+ // // boot(app, __dirname, function (err) {
71+ // // if (err) throw err;
72+ // //
73+ // // // start the server if `$ node server.js`
74+ // // if (require.main === module)
75+ // // app.start();
76+ // // });
77+
78+
79+
80+
181var loopback = require ( 'loopback' ) ;
282var boot = require ( 'loopback-boot' ) ;
3- var path = require ( 'path' ) ;
483
5- var app = module . exports = loopback ( ) ;
84+ var http = require ( 'http' ) ;
85+ var https = require ( 'https' ) ;
86+ var sslConfig = require ( './private/ssl_cert' ) ;
687
7- app . start = function ( ) {
8- // start the web server
9- return app . listen ( function ( ) {
10- app . emit ( 'started' ) ;
11- var baseUrl = app . get ( 'url' ) . replace ( / \/ $ / , '' ) ;
12- console . log ( 'Web server listening at: %s' , baseUrl ) ;
13- if ( app . get ( 'loopback-component-explorer' ) ) {
14- var explorerPath = app . get ( 'loopback-component-explorer' ) . mountPath ;
15- console . log ( 'Browse your REST API at %s%s' , baseUrl , explorerPath ) ;
16- }
17- } ) ;
18- } ;
88+ var app = module . exports = loopback ( ) ;
1989
20- //var staticPath = null;
21- //
22- //staticPath = path.resolve(__dirname, '../client/');
23- //console.log("Running app in development mode");
24- //
25- //app.use(loopback.static(staticPath));
90+ // boot scripts mount components like REST API
91+ boot ( app , __dirname ) ;
2692
27- // Bootstrap the application, configure models, datasources and middleware.
28- // Sub-apps like REST API are mounted via boot scripts.
29- boot ( app , __dirname , function ( err ) {
30- if ( err ) throw err ;
93+ app . start = function ( httpOnly ) {
94+ if ( httpOnly === undefined ) {
95+ httpOnly = process . env . HTTP ;
96+ }
97+ var server = null ;
98+ if ( ! httpOnly ) {
99+ var options = {
100+ key : sslConfig . privateKey ,
101+ cert : sslConfig . certificate ,
102+ } ;
103+ server = https . createServer ( options , app ) ;
104+ } else {
105+ server = http . createServer ( app ) ;
106+ }
107+ server . listen ( app . get ( 'port' ) , function ( ) {
108+ var baseUrl = ( httpOnly ? 'http://' : 'https://' ) + app . get ( 'host' ) + ':' + app . get ( 'port' ) ;
109+ app . emit ( 'started' , baseUrl ) ;
110+ console . log ( 'LoopBack server listening @ %s%s' , baseUrl , '/' ) ;
111+ if ( app . get ( 'loopback-component-explorer' ) ) {
112+ var explorerPath = app . get ( 'loopback-component-explorer' ) . mountPath ;
113+ console . log ( 'Browse your REST API at %s%s' , baseUrl , explorerPath ) ;
114+ }
115+ } ) ;
116+ return server ;
117+ } ;
31118
32- // start the server if `$ node server.js`
33- if ( require . main === module )
34- app . start ( ) ;
35- } ) ;
119+ // start the server if `$ node server.js`
120+ if ( require . main === module ) {
121+ app . start ( ) ;
122+ }
0 commit comments