Creating a service ================== Now that we have our :doc:`./creating`, we can create a new API endpoint to store messages. Generating a service -------------------- In Feathers any API endpoint is represented as a :doc:`../../api/services`, which we already learned about in the :doc:`../basics/services`. To generate a new service, we can run: .. code-block:: sh feathers generate service First we have to choose what kind of service we’d like to create. You can choose amongst many databases and ORMs but for this guide we will go with the default `NeDB `_. NeDB is a database that stores its data locally in a file and requires no additional configuration or database server. Next, when asked for the name of the service, enter ``messages``. Then keep the default path (``/messages``) by pressing enter. The *database connection string* can also be answered with the default. (In this case of NeDB, this is the path where it should store its database files.) Confirming the last prompt will generate some files and wire our service up: .. figure:: ./assets/service.png :alt: Final Configuration Final Configuration Et voilà! We have a fully functional REST and real-time API for our messages. The generated files ------------------- As we can see, several files were created: - ``src/services/messages/messages.service.js`` - The service setup file which registers the service in a :doc:`../basics/generator` - ``src/services/messages/messages.hooks.js`` - A file that returns an :doc:`../basics/hooks` that should be registered on the service. - ``src/models/messages.model.js`` - The model for our messages. Since we are using NeDB, this will simply instantiate the filesystem database. - ``test/services/messages.test.js`` - A Mocha test for the service. Initially, it only tests that the service exists. Testing the API --------------- If we now start our API with .. code-block:: sh npm start We can go to `localhost:3030/messages `_ and will see an (empty) response from our new messages service. We can also ``POST`` new messages and ``PUT``, ``PATCH`` and ``DELETE`` existing messages (via ``/messages/<_id>``), for example from the command line using `CURL `_: .. code-block:: sh curl 'http://localhost:3030/messages/' -H 'Content-Type: application/json' --data-binary '{ "text": "Hello from the command line!" }' Or with a REST client, e.g. \ `Postman `_, using this button: |Run in Postman| If we now go to `localhost:3030/messages `_ again we will see the newly created message(s). What’s next? ------------ With just one command, we created a fully functional REST and real-time API endpoint. Next, let’s :doc:`./authentication` and make sure messages only go to users that are allowed to see them. .. |Run in Postman| image:: https://run.pstmn.io/button.svg :target: https://app.getpostman.com/run-collection/9668636a9596d1e4a496