There are two methods to create extensions in Directus.io:
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 havecreate-directus-extension
utility.
More info: https://docs.directus.io/extensions/creating-extensions.htmlSecond 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":
Create a folder under
<project>/extensions/endpoints/<extension name>
, let us say extension name ishello
.Create file
index.js
insidehello
folder.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!'));
};
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 ...