|
const loopback = require('loopback'); |
|
const boot = require('loopback-boot'); |
|
const path = require('path'); |
|
const raygun = require('raygun'); |
|
const env = process.env.NODE_ENV || 'development'; |
|
|
|
const app = module.exports = loopback(); |
|
|
|
app.start = function() { |
|
|
|
// start the web server |
|
return app.listen(function() { |
|
var baseUrl = app.get('url').replace(/\/$/, ''); |
|
var angularPath = './'; |
|
var explorerPath; |
|
|
|
app.emit('started'); |
|
|
|
console.log('Web server listening at: %s', baseUrl); |
|
|
|
if (env !== 'production' && |
|
app.get('loopback-component-explorer')) { |
|
|
|
explorerPath = app.get('loopback-component-explorer').mountPath; |
|
console.log(`Browse your REST API at ${baseUrl}${explorerPath}`); |
|
} |
|
|
|
mountAngular(angularPath); |
|
}); |
|
}; |
|
|
|
// Bootstrap the application, configure models, datasources and middleware. |
|
// Sub-apps like REST API are mounted via boot scripts. |
|
boot(app, __dirname, function(err) { |
|
if (err) throw err; |
|
|
|
// start the server if `$ node server.js` |
|
if (require.main === module) |
|
app.start(); |
|
}); |
|
|
|
|
|
function mountAngular(mountPath) { |
|
const staticPath = path.resolve(__dirname, mountPath); |
|
const raygunClient = new raygun.Client().init({ |
|
apiKey: 'API_KEY_HERE' |
|
}); |
|
|
|
app.use(loopback.static(staticPath)); |
|
|
|
// have angular function in HTML5 mode |
|
app.use('/*', function(req, res, next) { |
|
|
|
if (req.originalUrl.includes('api')) { |
|
return next(); |
|
} |
|
if (req.originalUrl.includes('js.map')) { |
|
return next(); |
|
} |
|
|
|
res.sendFile(staticPath + '/index.html'); |
|
}); |
|
|
|
// This is how we add the error handler 'catcher' for raygun |
|
app.get('remoting').errorHandler = { |
|
handler: function(error, req, res, next) { |
|
raygunClient.send(error); |
|
|
|
//delegate the task down the line |
|
next(); |
|
}, |
|
disableStackTrace: env !== 'production' |
|
}; |
|
} |