Application

GitHub stars
npm version
Changelog

  1. $ npm install @feathersjs/feathers --save

The core @feathersjs/feathers module provides the ability to initialize new Feathers application instances. It works in Node, React Native and the browser (see the client chapter for more information). Each instance allows for registration and retrieval of services, hooks, plugin configuration, and getting and setting configuration options. An initialized Feathers application is referred to as the app object.

  1. const feathers = require('@feathersjs/feathers');
  2. const app = feathers();

.use(path, service)

app.use(path, service) -> app allows registering a service object on a given path.

  1. // Add a service.
  2. app.use('/messages', {
  3. get(id) {
  4. return Promise.resolve({
  5. id,
  6. text: `This is the ${id} message!`
  7. });
  8. }
  9. });

.service(path)

app.service(path) -> service returns the wrapped service object for the given path. Feathers internally creates a new object from each registered service. This means that the object returned by app.service(path) will provide the same methods and functionality as your original service object but also functionality added by Feathers and its plugins like service events and additional methods. path can be the service name with or without leading and trailing slashes.

  1. const messageService = app.service('messages');
  2. messageService.get('test').then(message => console.log(message));
  3. app.use('/my/todos', {
  4. create(data) {
  5. return Promise.resolve(data);
  6. }
  7. });
  8. const todoService = app.service('my/todos');
  9. // todoService is an event emitter
  10. todoService.on('created', todo =>
  11. console.log('Created todo', todo)
  12. );

.hooks(hooks)

app.hooks(hooks) -> app allows registration of application-level hooks. For more information see the application hooks section in the hooks chapter.

.publish([event, ] publisher)

app.publish([event, ] publisher) -> app registers a global event publisher. For more information see the channels publishing chapter.

.configure(callback)

app.configure(callback) -> app runs a callback function that gets passed the application object. It is used to initialize plugins or services.

  1. function setupService(app) {
  2. app.use('/todos', todoService);
  3. }
  4. app.configure(setupService);

.listen(port)

app.listen([port]) -> HTTPServer starts the application on the given port. It will set up all configured transports (if any) and then run app.setup(server) (see below) with the server object and then return the server object.

listen will only be available if a server side transport (REST, Socket.io or Primus) has been configured.

.setup([server])

app.setup([server]) -> app is used to initialize all services by calling each services .setup(app, path) method (if available).
It will also use the server instance passed (e.g. through http.createServer) to set up SocketIO (if enabled) and any other provider that might require the server instance.

Normally app.setup will be called automatically when starting the application via app.listen([port]) but there are cases when it needs to be called explicitly.

.set(name, value)

app.set(name, value) -> app assigns setting name to value.

.get(name)

app.get(name) -> value retrieves the setting name. For more information on server side Express settings see the Express documentation.

  1. app.set('port', 3030);
  2. app.listen(app.get('port'));

.on(eventname, listener)

Provided by the core NodeJS EventEmitter .on. Registers a listener method (function(data) {}) for the given eventname.

  1. app.on('login', user => console.log('Logged in', user));

.emit(eventname, data)

Provided by the core NodeJS EventEmitter .emit. Emits the event eventname to all event listeners.

  1. app.emit('myevent', {
  2. message: 'Something happened'
  3. });
  4. app.on('myevent', data => console.log('myevent happened', data));

.removeListener(eventname, [ listener ])

Provided by the core NodeJS EventEmitter .removeListener. Removes all or the given listener for eventname.

.mixins

app.mixins contains a list of service mixins. A mixin is a callback ((service, path) => {}) that gets run for every service that is being registered. Adding your own mixins allows to add functionality to every registered service.

  1. const feathers = require('@feathersjs/feathers');
  2. const app = feathers();
  3. // Mixins have to be added before registering any services
  4. app.mixins.push((service, path) => {
  5. service.sayHello = function() {
  6. return `Hello from service at '${path}'`;
  7. }
  8. });
  9. app.use('/todos', {
  10. get(id) {
  11. return Promise.resolve({ id });
  12. }
  13. });
  14. app.service('todos').sayHello();
  15. // -> Hello from service at 'todos'

.services

app.services contains an object of all services keyed by the path they have been registered via app.use(path, service). This allows to return a list of all available service names:

  1. const servicePaths = Object.keys(app.services);
  2. servicePaths.forEach(path => {
  3. const service = app.service(path);
  4. console.log(path, service);
  5. });

Note: To retrieve services, the app.service(path) method should be used (not app.services.path directly).

A Feathers client does not know anything about the server it is connected to. This means that app.services will not automatically contain all services available on the server. Instead, the server has to provide the list of its services, e.g. through a custom service:

  1. app.use('/info', {
  2. async find() {
  3. return {
  4. services: Object.keys(app.services)
  5. }
  6. }
  7. });

.defaultService

app.defaultService can be a function that returns an instance of a new standard service for app.service(path) if there isn’t one registered yet.

  1. const memory = require('feathers-memory');
  2. // For every `path` that doesn't have a service automatically return a new in-memory service
  3. app.defaultService = function(path) {
  4. return memory();
  5. }

This is used by the client transport adapters to automatically register client side services that talk to a Feathers server.