Develop a local extension in directus

There are two methods to create extensions in Directus.io:

  1. npm packages, which can be installed from the npm registry, from a tarball, from a git repository or any other means supported by npm.

    Documentation is straightforward for this method, we even have create-directus-extension utility.
    More info: https://docs.directus.io/extensions/creating-extensions.html

  2. Second method is to develop extensions locally inside the main project itself in extensions folder, more on this below.

On scaffolding a directus.io project, a folder is created by the name extensions, which can be used to develop local extensions without publishing to npm.

Let us create an endpoints extension "locally":

  1. Create a folder under <project>/extensions/endpoints/<extension name> , let us say extension name is hello.

  2. Create file index.js inside hello folder.

  3. Copy below code in index.js

module.exports = (router) => {
    router.get('/', (req, res) => res.send('Hello, World!'));
    router.get('/bob', (req, res) => res.send('Hello, Bob!'));
};
  1. After npx directus start , following endpoints should be working.
    http://0.0.0.0:8055/hello
    http://0.0.0.0:8055/hello/bob

More info on customising API Endpoints https://docs.directus.io/extensions/endpoints.html#example-recipes

As you make changes to index.js , changes won't reflect automatically without restarting the server, if you want Live Reloading, follow instructions given below,

When working on extensions, try setting the EXTENSIONS_AUTO_RELOAD environment variable. This will make the API reload extensions on changes automatically.


Stay tuned, will post more articles for each type of extension..

Write a comment ...

Rajat singla

Show your support

Give me your money! new line Shift enter Shift enter 2

Recent Supporters

Write a comment ...