React Nx Tutorial - Step 5: Add Node Application Implementing API

Nx.dev Tutorial | React | Step 5: Add Node Application Implementing API

The requests fail because the API has not been created yet. Using Nx you can develop node applications next to your React applications. You can use same commands to run and test them. You can share code between the backend and the frontend. Use this capability to implement the API service.

Add Express Plugin to Your Workspace

Nx is an open platform with plugins for many modern tools and frameworks. To see some plugins, run npx nx list:

>  NX  Installed plugins:

  @nrwl/cypress (builders,schematics)
  @nrwl/jest (builders,schematics)
  @nrwl/linter (builders)
  @nrwl/react (schematics)
  @nrwl/web (builders,schematics)
  @nrwl/workspace (builders,schematics)


>  NX  Also available:

  @nrwl/angular (schematics)
  @nrwl/express (builders,schematics)
  @nrwl/nest (builders,schematics)
  @nrwl/next (builders,schematics)
  @nrwl/node (builders,schematics)
  @nrwl/nx-plugin (builders,schematics)
  @nrwl/storybook (builders,schematics)


>  NX  Community plugins:

  @nxtend/ionic-react - An Nx plugin for developing Ionic React applications and libraries
  @angular-architects/ddd - Nx plugin for structuring a monorepo with domains and layers
  ...

Now run npx nx list @nrwl/express, and you will see:

>  NX   NOTE  @nrwl/express is not currently installed

  Use "yarn add --dev @nrwl/express" to add new capabilities

Add the dependency:

npm install --save-dev @nrwl/express

or

yarn add --dev @nrwl/express

@nrwl/express also added @nrwl/node. Run npx nx list @nrwl/express and npx nx list @nrwl/node to see what those plugins provide.

Generate an Express Application

Run the following to generate a new Express application:

npx nx g @nrwl/express:app api --frontendProject=todos

After this is done, you should see something like this:

myorg/
├── apps/
│   ├── todos/
│   ├── todos-e2e/
│   └── api/
│       ├── src/
│       │   ├── app/
│       │   ├── assets/
│       │   ├── environments/
│       │   │   ├── environment.ts
│       │   │   └── environment.prod.ts
│       │   └── main.ts
│       ├── jest.conf.js
│       ├── proxy.conf.json
│       ├── tsconfig.app.json
│       ├── tsconfig.json
│       └── tsconfig.spec.json
├── libs/
├── tools/
├── workspace.json
├── nx.json
├── package.json
└── tsconfig.json

The apps directory is where Nx places anything you can run: frontend applications, backend applications, e2e test suites. That's why the api application appeared there.

You can run:

  • npx nx serve api to serve the application
  • npx nx build api to build the application
  • npx nx test api to test the application

Add a file apps/api/src/app/todos.ts.

1import { Express } from 'express';
2
3interface Todo {
4  title: string;
5}
6
7const todos: Todo[] = [{ title: 'Todo 1' }, { title: 'Todo 2' }];
8
9export function addTodoRoutes(app: Express) {
10  app.get('/api/todos', (req, resp) => resp.send(todos));
11  app.post('/api/addTodo', (req, resp) => {
12    const newTodo = {
13      title: `New todo ${Math.floor(Math.random() * 1000)}`,
14    };
15    todos.push(newTodo);
16    resp.send(newTodo);
17  });
18}

Here, you are building an Express application with Nx. Nx also comes with Nest support, and you can also use any other node library you want.

Next update apps/api/src/main.ts to register the routes

1import * as express from 'express';
2import { addTodoRoutes } from './app/todos';
3
4const app = express();
5
6app.get('/api', (req, res) => {
7  res.send({ message: 'Welcome to api!' });
8});
9addTodoRoutes(app);
10
11const port = process.env.port || 3333;
12const server = app.listen(port, () => {
13  console.log(`Listening at http://localhost:${port}/api`);
14});
15server.on('error', console.error);

!!!!! Run "npx nx serve api" and open http://localhost:3333/api/todos. What do you see? !!!!! [{"title":"Todo 1"},{"title":"Todo 2"}] Blank screen 404