xfscrypt
xfscrypt

Reputation: 276

Loopback: app is undefined

I am having a custom script in server/middleware/robots.js with the following content:

module.exports = function(app) {
  app.get('/robots.txt', function (req, res) {
    res.type('text/plain');

    if (app.settings.env === 'production') {
      res.send("User-agent: *\nAllow: /");
    } else {
      res.send("User-agent: *\nDisallow: /");
    }
  });
};

However I am getting the error message that app is undefined.

I tried adding the following line at the bottom of server.js:

module.exports = app;

but no luck.

When I remove the module.exports line and require app from ../server I get the following error:

[2018-07-07T09:51:30.077Z] error: uncaughtException: Middleware factory must be a function

How do I access app outside server.js? I followed the documentation closely but I am not able to do this.

Upvotes: 0

Views: 182

Answers (2)

Ajaykkumar R
Ajaykkumar R

Reputation: 261

Try importing the server.js into your robot.js file

    const app = require('../server.js')

And also export your app in server.js

`   const loopback = require('loopback');` 

    const app = module.exports = loopback();

Upvotes: 2

Antonio Trapani
Antonio Trapani

Reputation: 811

Have you already tried to import the server?

const app = require('../server')

module.exports = function() {
  app.get('/robots.txt', function (req, res) {
    res.type('text/plain');

    if (app.settings.env === 'production') {
      res.send("User-agent: *\nAllow: /");
    } else {
      res.send("User-agent: *\nDisallow: /");
    }
  });
};

Upvotes: 1

Related Questions