Our first Feathers application ============================== Now that we are :doc:`./setup` we can create our first Feathers application. It will work in both, NodeJS and the browser. First, let’s create a new folder for all our examples to run in: .. code-block:: sh mkdir feathers-basics cd feathers-basics Since any Feathers application is a Node application, we can create a default `package.json `_ using ``npm``: .. code-block:: sh npm init --yes Installing Feathers ------------------- Feathers can be installed like any other Node module by installing the `@feathersjs/feathers `_ package through `npm `_. The same package can also be used with a module loader like Browserify or Webpack and React Native. .. code-block:: sh npm install @feathersjs/feathers --save .. .. note:: All Feathers core modules are in the ``@feathersjs`` npm namespace. Your first app -------------- The base of any Feathers application is the :doc:`../../api/application` which can be created like this: .. code:: js const feathers = require('@feathersjs/feathers'); const app = feathers(); This application object has several methods, most importantly it allows us to register services. We will learn more about services in the next chapter, for now let’s register and use a simple service that has only a ``get`` method by creating an ``app.js`` file (in the current folder) like this: .. code:: js const feathers = require('@feathersjs/feathers'); const app = feathers(); // Register a simple todo service that returns the name and some text app.use('todos', { async get(name) { // Return an object in the form of { name, text } return { name, text: `You have to do ${name}` }; } }); // A function that gets and logs a todo from the service async function getTodo(name) { // Get the service we registered above const service = app.service('todos'); // Call the `get` method with a name const todo = await service.get(name); // Log the todo we got back console.log(todo); } getTodo('dishes'); We can run it with .. code-block:: sh node app.js And should see .. code:: js { name: 'dishes', text: 'You have to do dishes' } .. .. tip:: For more information about the Feathers application object see the :doc:`../../api/application`. In the browser -------------- The Feathers application we created above can also run just the same in the browser. The easiest way to load Feathers here is through a `` And an ``client.js`` looking like this: .. code:: js const app = feathers(); // Register a simple todo service that return the name and a text app.use('todos', { async get(name) { // Return an object in the form of { name, text } return { name, text: `You have to do ${name}` }; } }); // A function that gets and logs a todo from the service async function logTodo(name) { // Get the service we registered above const service = app.service('todos'); // Call the `get` method with a name const todo = await service.get(name); // Log the todo we got back console.log(todo); } logTodo('dishes'); You may notice that it is pretty much the same as our ``app.js`` for Node except the missing ``feathers`` import (since it is already available as a global variable). If you now go to `localhost:8080 `_ with the console open you will also see the result logged. .. note:: You can also load Feathers with a module loader like Webpack or Browserify. For more information see the :doc:`../../api/client`. What’s next? ------------ In this chapter we created our first Feathers application with a simple service that works in Node and the browser. Next, let’s learn more about :doc:`services`.