Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var loopback = require('loopback');
- var boot = require('loopback-boot');
- var app = module.exports = loopback();
- // Set up the /favicon.ico
- app.use(loopback.favicon());
- // request pre-processing middleware
- app.use(loopback.compress());
- // // PASSPORT
- // var loopbackPassport = require('loopback-component-passport');
- // var PassportConfigurator = loopbackPassport.PassportConfigurator;
- // var passportConfigurator = new PassportConfigurator(app);
- // var config = {};
- // try {
- // config = require('../providers.json');
- // } catch (err) {
- // console.trace(err);
- // process.exit(1); // fatal
- // }
- app.use(loopback.token()); // this calls getCurrentContext
- app.use(loopback.context()); // the context is started here
- app.use(function (req, res, next) {
- if (!req.accessToken) return next();
- app.models.MyUser.findById(req.accessToken.userId, function(err, user) {
- if (err) return next(err);
- if (!user) return next(new Error('No user with this access token was found.'));
- res.locals.currentUser = user;
- var loopbackContext = loopback.getCurrentContext();
- if (loopbackContext) loopbackContext.set('currentUser', user);
- next();
- });
- });
- // boot scripts mount components like REST API
- boot(app, __dirname);
- // -- Mount static files here--
- // All static middleware should be registered at the end, as all requests
- // passing the static middleware are hitting the file system
- // Example:
- // var path = require('path');
- // app.use(loopback.static(path.resolve(__dirname, '../client')));
- // 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());
- // The ultimate error handler.
- app.use(loopback.errorHandler());
- app.start = function() {
- // start the web server
- return app.listen(function() {
- app.emit('started');
- console.log('Web server listening at: %s', app.get('url'));
- });
- };
- // start the server if `$ node server.js`
- if (require.main === module) {
- app.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement