This PR updates examples in `.md` files (both docs and blog posts) to use positional args. Nx 20 changes the position arg to be either `directory` for apps/libs or `path` for artifacts (e.g. components). So before you'd do this: ``` nx g app myapp --directory=apps/myapp nx g lib mylib --directory=libs/mylib nx g lib mylib --directory=libs/nested/mylib nx g lib @acme/foo --directory=libs/@acme/foo --importPath=@acme/foo nx g component foo --directory=libs/ui/src/foo --pascalCaseFiles ``` Will now be simplified to ``` nx g app apps/myapp nx g lib libs/mylib nx g lib libs/nested/mylib nx g lib libs/@acme/foo # name and import path are both "@acme/foo" nx g component libs/ui/src/foo/Foo ``` For cases where `name` and `importPath` need to be changed, you can always manually specify them. ``` nx g lib libs/nested/foo # name is foo nx g lib libs/nested/foo --name=nested-foo # specify name with prefix nx g lib libs/@acme/foo --name # use "foo" as name and don't match importPath nx g lib libs/@internal/foo --importPath=@acme/foo # different importPath from name <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
105 lines
3.7 KiB
Markdown
105 lines
3.7 KiB
Markdown
# Add a New Fastify Project
|
|
|
|
{% 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 %}
|
|
|
|
## Create a New Workspace with a Fastify App
|
|
|
|
If you're starting from scratch, you can use a preset to get you started quickly.
|
|
|
|
```shell
|
|
npx create-nx-workspace@latest --preset=node-monorepo --framework=fastify --appName=fastify-api
|
|
```
|
|
|
|
Then you can skip to the [Create a Library](#create-a-library) section.
|
|
|
|
If you are adding Fastify to an existing repo, continue to the next section.
|
|
|
|
## Install the Node Plugin
|
|
|
|
```shell {% skipRescope=true %}
|
|
nx add @nx/node
|
|
```
|
|
|
|
## Create an Application
|
|
|
|
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](/deprecated/as-provided-vs-derived) for more details.
|
|
{% /callout %}
|
|
|
|
```shell
|
|
nx g @nx/node:app apps/fastify-api
|
|
```
|
|
|
|
Serve the API by running
|
|
|
|
```shell
|
|
nx serve fastify-api
|
|
```
|
|
|
|
This starts the application on localhost:3000/api by default.
|
|
|
|
## Create a Library
|
|
|
|
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](/deprecated/as-provided-vs-derived) for more details.
|
|
{% /callout %}
|
|
|
|
```shell
|
|
nx g @nx/node:lib libs/my-lib
|
|
```
|
|
|
|
Once the library is created, update the following files.
|
|
|
|
```typescript {% fileName="libs/my-lib/src/lib/my-lib.ts" %}
|
|
export function someFunction(): string {
|
|
return 'some function';
|
|
}
|
|
```
|
|
|
|
```typescript {% fileName="apps/fastify-app/src/app/routes/root.ts" %}
|
|
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.
|
|
|
|
## More Documentation
|
|
|
|
- [@nx/node](/nx-api/node)
|
|
- [Using Mongo with Fastify](/showcase/example-repos/mongo-fastify)
|
|
- [Using Redis with Fastify](/showcase/example-repos/redis-fastify)
|
|
- [Using Postgres with Fastify](/showcase/example-repos/postgres-fastify)
|
|
- [Using PlanetScale with Serverless Fastify](/showcase/example-repos/serverless-fastify-planetscale)
|
|
- [Fastify](https://fastify.dev/)
|