中间件

时间:2017-08-03 04:56:08

标签: loopbackjs

有没有办法在我们的代码中使用用户(模型)方法,如(login& logout)?就像在我的情况下,我想在中间件的路由阶段中定义的中间件中使用User.login(....)方法。 我试图以这种方式在中间件文件中导入User模型。

 var loopback = require('loopback');
 var boot = require('loopback-boot');
 var app = module.exports = loopback();

 var User = app.models.user;

它给了我错误" app未定义"。 请告诉我有没有办法在我的中间件中使用登录等方法。

谢谢

 'use strict';
  var mysql = require('mysql')
  var loopback = require('loopback');
  var boot = require('loopback-boot');
  var app = module.exports = loopback();

  app.start = function() {
  // start the web server
   return app.listen(function() {
      app.emit('started');
   var baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
  var explorerPath = app.get('loopback-component-explorer').mountPath;
  console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
      }
  });
 };

//引导应用程序,配置模型,数据源和中间件。 // REST API等子应用程序通过启动脚本安装。

 boot(app, __dirname, function(err) {
 if (err) throw err;

//如果$ node server.js

启动服务器
 if (require.main === module)
   app.start(); 
 });

1 个答案:

答案 0 :(得分:0)

You need to bootstrap the application in advance before using the var loopback = require('loopback'); var boot = require('loopback-boot'); var app = module.exports = loopback(); boot(app, __dirname, function(err) { // You can start your application here app.start(); // Models have been loaded to app object after boot(). You can use it here if you want var User = app.models.user; });

Revoke USB debugging autorizations

From their official document

The LoopBack bootstrapper, loopback-boot, performs application initialization (also called bootstrapping). When an application starts, the bootstrapper:

  • Configures data sources.
  • Defines custom models Configures models and attaches models to data-sources.
  • Configures application settings
  • Runs boot scripts in the /server/boot directory.

You may want to look at their sample server.js for better reference