0

I am trying to understand the following code taken from a service created in a feathersjs app.

// Initializes the `users` service on path `/users`
const createService = require('feathers-knex');
const createModel = require('../../models/users.model');
const hooks = require('./users.hooks');
const filters = require('./users.filters');

module.exports = function () {
  const app = this;
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    name: 'users',
    Model,
    paginate
  };

  // Initialize our service with any options it requires
  app.use('/users', createService(options));

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('users');

  service.hooks(hooks);

  if (service.filter) {
    service.filter(filters);
  }
};

This file is then imported as follows:

// many requires..
const feathers = require('feathers');
// more requires
const services = require('./services');
// other requires
const app = feathers();

Can someone explain as to what does the line

const app = this

do in the code that is creating the service?

1 Answers1

3

It assigns the value of this to a variable (well, a constant) with a name that more clearly describes the value than this does. It is designed to make the code easier for maintainers to understand.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205