Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var loopback = require('loopback');
  2. var boot = require('loopback-boot');
  3. var app = module.exports = loopback();
  4.  
  5. // Set up the /favicon.ico
  6. app.use(loopback.favicon());
  7.  
  8. // request pre-processing middleware
  9. app.use(loopback.compress());
  10.  
  11. // // PASSPORT
  12. // var loopbackPassport = require('loopback-component-passport');
  13. // var PassportConfigurator = loopbackPassport.PassportConfigurator;
  14. // var passportConfigurator = new PassportConfigurator(app);
  15. // var config = {};
  16. // try {
  17. //     config = require('../providers.json');
  18. // } catch (err) {
  19. //     console.trace(err);
  20. //     process.exit(1); // fatal
  21. // }
  22.  
  23. app.use(loopback.token()); // this calls getCurrentContext
  24. app.use(loopback.context()); // the context is started here
  25.  
  26. app.use(function (req, res, next) {
  27.   if (!req.accessToken) return next();
  28.  
  29.   app.models.MyUser.findById(req.accessToken.userId, function(err, user) {
  30.     if (err)   return next(err);
  31.     if (!user) return next(new Error('No user with this access token was found.'));
  32.     res.locals.currentUser = user;
  33.  
  34.     var loopbackContext = loopback.getCurrentContext();
  35.     if (loopbackContext) loopbackContext.set('currentUser', user);
  36.     next();
  37.   });
  38. });
  39.  
  40.  
  41.  
  42.  
  43. // boot scripts mount components like REST API
  44. boot(app, __dirname);
  45.  
  46. // -- Mount static files here--
  47. // All static middleware should be registered at the end, as all requests
  48. // passing the static middleware are hitting the file system
  49. // Example:
  50. //   var path = require('path');
  51. //   app.use(loopback.static(path.resolve(__dirname, '../client')));
  52.  
  53. // Requests that get this far won't be handled
  54. // by any middleware. Convert them into a 404 error
  55. // that will be handled later down the chain.
  56. app.use(loopback.urlNotFound());
  57.  
  58. // The ultimate error handler.
  59. app.use(loopback.errorHandler());
  60.  
  61. app.start = function() {
  62.   // start the web server
  63.   return app.listen(function() {
  64.     app.emit('started');
  65.     console.log('Web server listening at: %s', app.get('url'));
  66.   });
  67. };
  68.  
  69. // start the server if `$ node server.js`
  70. if (require.main === module) {
  71.   app.start();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement