{% youtube src="https://www.youtube.com/embed/LHLW0b4fr2w" title="Easy, Modular Node Applications!" width="100%" /%}
Supported Features
Because we are using an Nx plugin for Fastify, all the features of Nx are available.
{% pill url="/features/run-tasks" %}✅ Run Tasks{% /pill %} {% pill url="/features/cache-task-results" %}✅ Cache Task Results{% /pill %} {% pill url="/ci/features/remote-cache" %}✅ Share Your Cache{% /pill %} {% pill url="/features/explore-graph" %}✅ Explore the Graph{% /pill %} {% pill url="/ci/features/distribute-task-execution" %}✅ Distribute Task Execution{% /pill %} {% pill url="/getting-started/editor-setup" %}✅ Integrate with Editors{% /pill %} {% pill url="/features/automate-updating-dependencies" %}✅ Automate Updating Nx{% /pill %} {% pill url="/features/enforce-module-boundaries" %}✅ Enforce Module Boundaries{% /pill %} {% pill url="/features/generate-code" %}✅ Use Code Generators{% /pill %} {% pill url="/features/automate-updating-dependencies" %}✅ Automate Updating Framework Dependencies{% /pill %}
If you're starting from scratch, you can use a preset to get you started quickly.
npx create-nx-workspace@latest --preset=node-monorepo --framework=fastify --appName=fastify-api
Then you can skip to the Create a Library section.
If you are adding Fastify to an existing repo, continue to the next section.
nx add @nx/node
Use the app
generator to create a new Fastify app.
{% callout type="note" title="Directory Flag Behavior Changes" %}
The command below uses the as-provided
directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived
option, omit the --directory
flag. See the as-provided vs. derived documentation for more details.
{% /callout %}
nx g @nx/node:app fastify-api --directory=apps/fastify-api
Serve the API by running
nx serve fastify-api
This starts the application on localhost:3000/api by default.
To create a new library, run:
{% callout type="note" title="Directory Flag Behavior Changes" %}
The command below uses the as-provided
directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived
option, omit the --directory
flag. See the as-provided vs. derived documentation for more details.
{% /callout %}
nx g @nx/node:lib my-lib --directory=libs/my-lib
Once the library is created, update the following files.
export function someFunction(): string {
return 'some function';
}
import { someFunction } from '@my-org/my-lib';
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
export default async function (fastify: FastifyInstance) {
fastify.get(
'/',
async function (request: FastifyRequest, reply: FastifyReply) {
return { message: 'Hello API ' + someFunction };
}
);
}
Now when you serve your API, you'll see the content from the library being displayed.