FAQ

We’ve been collecting some commonly asked questions here. We’ll either be updating the guide directly, providing answers here, or both.

What Node versions does Feathers support

The latest version of Feathers and all plugins work with Node 6 and later. Node 6 will be supported until 2019-04-18 when the official support cycle ends.

The Feathers guides and applications generated by the CLI (@feathersjs/cli) use newer language features like async/await and require Node 8 or later.

How do I create custom methods?

One important thing to know about Feathers is that it only exposes the official service methods to clients. While you can add and use any service method on the server, it is not possible to expose those custom methods to clients.

Feathers is built around the REST architectural constraints and there are many good reasons for it. In general, almost anything that may require custom methods or RPC style actions can also be done either by creating a custom service or through hooks.

The benefits (like security, predictability, sending well defined real-time events) so far heavily outweighed the slight change in thinking required when conceptualizing your application logic.

Examples:

  • Send email action that does not store mail message in database.

Resources (services) don’t have to be database records. It can be any kind of resource (like the current weather for a city or creating an email which will send it). Sending emails is usually done with either a separate email service:

  1. app.use('/email', {
  2. create(data) {
  3. return sendEmail(data);
  4. }
  5. })

Or in a hook.

  • Place an order in e-commerce web site. Behind the scenes, there are many records will be inserted in one transaction: order_item, order_header, voucher_tracking etc.

This is what Feathers hooks are for. When creating a new order you also have a well defined hook chain:

  1. app.service('orders').hooks({
  2. before: {
  3. create: [
  4. validateData(),
  5. checkStock(),
  6. checkVoucher()
  7. ]
  8. },
  9. after: {
  10. create: [
  11. chargePayment(), // hook that calls `app.service('payment').create()`
  12. sendEmail(), // hook that calls `app.service('email').create()`
  13. updateStock() // Update product stock here
  14. ]
  15. }
  16. })
  • A userService.resetPassword method

This can also be implemented as a password service that resets the password in the create method:

  1. const crypto = require('crypto');
  2. class PasswordService {
  3. create(data) {
  4. const userId = data.user_id;
  5. const userService = this.app.service('user');
  6. return userService.patch(userId, {
  7. passwordToken: crypto.randomBytes(48)
  8. }).then(user => sendEmail(user))
  9. }
  10. setup(app) {
  11. this.app = app;
  12. }
  13. }

How do I do nested or custom routes?

Normally we find that they actually aren’t needed and that it is much better to keep your routes as flat as possible. For example something like users/:userId/posts is - although nice to read for humans - actually not as easy to parse and process as the equivalent /posts?userId=<userid> that is already supported by Feathers out of the box. Additionaly, this will also work much better when using Feathers through websocket connections which do not have a concept of routes at all.

However, nested routes for services can still be created by registering an existing service on the nested route and mapping the route parameter to a query parameter like this:

  1. app.use('/posts', postService);
  2. app.use('/users', userService);
  3. // re-export the posts service on the /users/:userId/posts route
  4. app.use('/users/:userId/posts', app.service('posts'));
  5. // A hook that updates `data` with the route parameter
  6. function mapUserIdToData(context) {
  7. if(context.data && context.params.route.userId) {
  8. context.data.userId = context.params.route.userId;
  9. }
  10. }
  11. // For the new route, map the `:userId` route parameter to the query in a hook
  12. app.service('users/:userId/posts').hooks({
  13. before: {
  14. find(context) {
  15. context.params.query.userId = context.params.route.userId;
  16. },
  17. create: mapUserIdToData,
  18. update: mapUserIdToData,
  19. patch: mapUserIdToData
  20. }
  21. })

Now going to /users/123/posts will call postService.find({ query: { userId: 123 } }) and return all posts for that user.

For more information about URL routing and parameters, refer to the Express chapter.

Note: URLs should never contain actions that change data (like post/publish or post/delete). This has always been an important part of the HTTP protocol and Feathers enforces this more strictly than most other frameworks. For example to publish a post you would call .patch(id, { published: true }).

Can you support another database?

Feathers database adapters implement 90% of the functionality you may need to use Feathers with certain databases and ORMs. However, even if your favourite database or ORM is not on the list or the adapter does not support specific functionality you are looking for, Feathers can still accomodate all your needs by writing your own services.

Important: To use Feathers properly it is very important to understand how services work and that all existing database adapters are just services that talk to the database themselves.

The why and how to write your own services is covered in the basics guide. In the generator a custom service can be created by running feathers generate service, choosing “A custom service” and then editing the <servicename>/<servicename>.class.js file to make the appropriate database calls.

If you would like to publish your own database adapter, first make sure there isn’t already a community maintained adapter for that database (many maintainers are happy to get some help, too). If not, you can run feathers generate plugin to create a new plugin. A reference implementation for a database adapter can be found in the feathers-memory repository. It is always possible for community maintained adapters to graduate into an official Feathers adapter, at the moment there are however no plans to add support for any new databases from the Feathers team directly.

I am not getting real-time events

Feathers Buzzard (@feathersjs/feathers@v3.0.0) introduced a new, more secure event system that does not send real-time events by default. If you are not getting real-time events on the client, it is usually a problem with the event channel setup.

Have a look a the example at feathersjs.com, the real-time basics guide and the channels documentation. If you are migrating from a previous version, also see the channels section int the migration guide.

The generated application already sets up a channels.js file that sends events to only authenticated users by default but can be modified to your needs according the the channels documentation.

How do I do search?

This depends on the database adapter you are using. See the search querying chapter for more information.

Why am I not getting JSON errors?

If you get a plain text error and a 500 status code for errors that should return different status codes, make sure you have the express.errorHandler() from the @feathersjs/express module configured as described in the Express errors chapter.

Why am I not getting the correct HTTP error code

See the above answer.

How can I do custom methods like findOrCreate?

Custom functionality can almost always be mapped to an existing service method using hooks. For example, findOrCreate can be implemented as a before-hook on the service’s get method. See this gist for an example of how to implement this in a before-hook.

How do I render templates?

Feathers works just like Express so it’s the exact same. We’ve created a helpful little guide right here. For protecting Express views with authentication, also see this guide.

If you are authenticating via oAuth but your API and frontend reside on different domains the cookie used by the authentication client can not be set. Instead, a query string redirect has to be used as shown in this gist.

How do I create channels or rooms

In Feathers channels are the way to send real-time events to only certain clients.

How do I do validation?

If your database/ORM supports a model or schema (ie. Mongoose or Sequelize) then you have 2 options.

The preferred way

You perform validation at the service level using hooks. This is better because it keeps your app database agnostic so you can easily swap databases without having to change your validations much.

If you write a bunch of small hooks that validate specific things it is easier to test and also slightly more performant because you can exit out of the validation chain early instead of having to go all the way to the point of inserting data into the database to find out if that data is invalid.

If you don’t have a model or schema then validating with hooks is currently your only option. If you come up with something different feel free to submit a PR!

The ORM way

With ORM adapters you can perform validation at the model level:

The nice thing about the model level validations is Feathers will return the validation errors to the client in a nice consistent format for you.

How do I do associations?

Similar to validation, it depends on if your database/ORM supports models or not.

The preferred way

For any of the feathers database/ORM adapters you can just use hooks to fetch data from other services.

This is a better approach because it keeps your application database agnostic and service oriented. By referencing the services (using app.service().find(), etc.) you can still decouple your app and have these services live on entirely separate machines or use entirely different databases without having to change any of your fetching code. We show how to associate data in a hook in the chat guide. An alternative are the fastJoin or populate in feathers-hooks-common.

The ORM way

With mongoose you can use the $populate query param to populate nested documents.

  1. // Find Hulk Hogan and include all the messages he sent
  2. app.service('user').find({
  3. query: {
  4. name: 'hulk@hogan.net',
  5. $populate: ['sentMessages']
  6. }
  7. });

With Sequelize you can do this:

  1. // Find Hulk Hogan and include all the messages he sent
  2. app.service('user').find({
  3. name: 'hulk@hogan.net',
  4. sequelize: {
  5. include: [{
  6. model: Message,
  7. where: { sender: Sequelize.col('user.id') }
  8. }]
  9. }
  10. });

Or set it in a hook as described here.

Sequelize models and associations

If you are using the Sequelize adapter, understanding SQL and Sequelize first is very important. See the associations section in the feathers-sequelize documentation for more information on how to associate models using the Sequelize Feathers adapter.

What about Koa/Hapi/X?

There are many other Node server frameworks out there like Koa, a “next generation web framework for Node.JS” using ES6 generator functions instead of Express middleware or HapiJS etc. Currently, Feathers is framework independent but only provides an Express integration for HTTP APIs. More frameworks may be supported in the future with direct Node HTTP being the most likely.

How do I access the request object in hooks or services?

In short, you shouldn’t need to. If you look at the hooks chapter you’ll see all the params that are available on a hook.

If you still need something from the request object (for example, the requesting IP address) you can simply tack it on to the req.feathers object as described here.

How do I mount sub apps?

It’s pretty much exactly the same as Express. More information can be found here.

How do I do some processing after sending the response to the user?

The hooks workflow allows you to handle these situations quite gracefully. It depends on the promise that you return in your hook. Here’s an example of a hook that sends an email, but doesn’t wait for a success message.

  1. function (context) {
  2. // Send an email by calling to the email service.
  3. context.app.service('emails').create({
  4. to: 'user@email.com',
  5. body: 'You are so great!'
  6. });
  7. // Send a message to some logging service.
  8. context.app.service('logging').create(context.data);
  9. // Return a resolved promise to immediately move to the next hook
  10. // and not wait for the two previous promises to resolve.
  11. return Promise.resolve(context);
  12. }

How do I debug my app

It’s really no different than debugging any other NodeJS app but you can refer to this blog post for more Feathers specific tips and tricks.

possible EventEmitter memory leak detected warning

This warning is not as bad as it sounds. If you got it from Feathers you most likely registered more than 64 services and/or event listeners on a Socket. If you don’t think there are that many services or event listeners you may have a memory leak. Otherwise you can increase the number in the Socket.io configuration via io.sockets.setMaxListeners(number) and with Primus via primus.setMaxListeners(number). number can be 0 for unlimited listeners or any other number of how many listeners you’d expect in the worst case.

Why can’t I pass params from the client?

When you make a call like:

  1. const params = { foo: 'bar' };
  2. client.service('users').patch(1, { admin: true }, params).then(result => {
  3. // handle response
  4. });

on the client the context.params object will only be available in your client side hooks. It will not be provided to the server. The reason for this is because context.params on the server usually contains information that should be server-side only. This can be database options, whether a request is authenticated, etc. If we passed those directly from the client to the server this would be a big security risk. Only the client side context.params.query and context.params.headers objects are provided to the server.

If you need to pass info from the client to the server that is not part of the query you need to add it to context.params.query on the client side and explicitly pull it out of context.params.query on the server side. This can be achieved like so:

  1. // client side
  2. client.hooks({
  3. before: {
  4. all: [
  5. context => {
  6. context.params.query.$client = {
  7. platform: 'ios',
  8. version: '1.0'
  9. };
  10. return context;
  11. }
  12. ]
  13. }
  14. });
  15. // server side, inside app.hooks.js
  16. const hooks = require('feathers-hooks-common');
  17. module.exports = {
  18. before: {
  19. all: [
  20. // remove values from context.params.query.$client and move them to context.params
  21. // so context.params.query.$client.version -> context.params.version
  22. // and context.params.query.$client is removed.
  23. hooks.client('version', 'platform')
  24. ]
  25. }
  26. }

My queries with null values aren’t working

When making a request using REST (HTTP) query string values don’t have any type information and will always be strings. Some database adapters that have a schema (like feathers-mongoose or feathers-sequelize) will try to convert values to the correct type but others (like feathers-mongodb) can’t. Additionally, null will always be a string and always has to be converted if you want to query for null. This can be done in a before hook:

  1. app.service('myservice').hooks({
  2. before: {
  3. find(context) {
  4. const { params: { query = {} } } = context;
  5. if(query.phone === 'null') {
  6. query.phone = null;
  7. }
  8. context.params.query = query;
  9. return context;
  10. }
  11. }
  12. });

Also see this issue.

Note: This issue does not happen when using websockets since it retains all type information.

Why are queries with arrays failing?

If you are using REST and queries with larger arrays (more than 21 items to be exact) are failing you are probably running into an issue with the querystring module which limits the size of arrays to 21 items by default. The recommended solution is to implement a custom query string parser function via app.set('query parser', parserFunction) with the arrayLimit option set to a higher value:

  1. var qs = require('qs');
  2. app.set('query parser', function (str) {
  3. return qs.parse(str, {
  4. arrayLimit: 100
  5. });
  6. });

For more information see the Express application settings @feathersjs/rest#88 and feathers-mongoose#205.

I always get a 404 for my custom middleware

Just like in Express itself, the order of middleware matters. If you registered a custom middleware outside of the generator, you have to make sure that it runs before the notFound() error midlleware.

How do I get OAuth working across different domains

The standard Feathers oAuth setup sets the JWT in a cookie which can only be passed between the same domain. If your frontend is running on a different domain you will have to use query string redirects as outlined in this Gist.

My configuration isn’t loaded

If you are running or requiring the Feathers app from a different folder Feathers configuration needs to be instructed where the configuration files for the app are located. Since it uses node-config this can be done by setting the NODE_CONFIG_DIR envorinment variable.

How do I set up HTTPS?

Check out the Feathers Express HTTPS docs.

Is Feathers production ready?

Yes! It’s being used in production by a bunch of companies from startups to fortune 500s. For some more details see this answer on Quora.