|
| 1 | +--- |
| 2 | +description: Learn how to set up a React Website that calls your Nitric API |
| 3 | +tags: |
| 4 | + - Websites |
| 5 | + - API |
| 6 | +languages: |
| 7 | + - typescript |
| 8 | + - javascript |
| 9 | +published_at: 2025-03-20 |
| 10 | +--- |
| 11 | + |
| 12 | +# Setting up a React Website calling your Nitric API |
| 13 | + |
| 14 | +This guide demonstrates how to use Nitric to create an API and a frontend website that interacts with it. We will be using Vite to create a React project and connect it to our Nitric API. The finished source can be found [here](https://github.com/nitrictech/examples/tree/main/v1/nitric-vite-react). |
| 15 | + |
| 16 | +## Create a new Nitric project |
| 17 | + |
| 18 | +The first step is to create a new Nitric TypeScript project using the [Nitric CLI](/reference/cli). |
| 19 | + |
| 20 | +```bash |
| 21 | +nitric new api-project ts-starter |
| 22 | +cd api-project |
| 23 | +npm install |
| 24 | +``` |
| 25 | + |
| 26 | +## Creating the frontend |
| 27 | + |
| 28 | +We will use Vite to set up a new React project for our frontend. |
| 29 | + |
| 30 | +```bash |
| 31 | +npm create vite@latest main-website -- --template react-ts |
| 32 | +``` |
| 33 | + |
| 34 | +Navigate to the project directory and install dependencies. |
| 35 | + |
| 36 | +```bash |
| 37 | +cd main-website |
| 38 | +npm install |
| 39 | +``` |
| 40 | + |
| 41 | +## Creating a Nitric API |
| 42 | + |
| 43 | +Review the API in your `services` directory, by default it echos hello `name`. |
| 44 | + |
| 45 | +```ts title: services/api.ts |
| 46 | +import { api } from '@nitric/sdk' |
| 47 | + |
| 48 | +const mainApi = api('main') |
| 49 | + |
| 50 | +mainApi.get('/hello/:name', async (ctx) => { |
| 51 | + const { name } = ctx.req.params |
| 52 | + ctx.res.body = `Hello ${name}` |
| 53 | + return ctx |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +## Configuring Nitric |
| 58 | + |
| 59 | +Add a website resource to your `nitric.yaml` file, you'll be configuring the base directory, and build/run commands. You can [learn more](https://nitric.io/docs/websites) about this configuration. |
| 60 | + |
| 61 | +<Note> |
| 62 | + Nitric websites are currently in preview, so we'll also enable this feature. |
| 63 | +</Note> |
| 64 | + |
| 65 | +```yaml title: nitric.yaml |
| 66 | +name: nitric-vite-react |
| 67 | +services: |
| 68 | + - basedir: '' |
| 69 | + match: services/*.ts |
| 70 | + runtime: node |
| 71 | + start: npm run dev:services $SERVICE_PATH |
| 72 | +batch-services: [] |
| 73 | +websites: |
| 74 | + - basedir: ./main-website |
| 75 | + # Since this is a Single Page Application (SPA), we need to redirect all requests to the index.html file. |
| 76 | + error: index.html |
| 77 | + build: |
| 78 | + command: npm run build |
| 79 | + output: dist |
| 80 | + dev: |
| 81 | + command: npm run dev -- --port 3000 |
| 82 | + url: http://localhost:3000 |
| 83 | +runtimes: |
| 84 | + node: |
| 85 | + dockerfile: ./node.dockerfile |
| 86 | + context: '' |
| 87 | + args: {} |
| 88 | +preview: |
| 89 | + - websites |
| 90 | +``` |
| 91 | +
|
| 92 | +## Updating the Website to call the API |
| 93 | +
|
| 94 | +Modify the `App.tsx` file in the `main-website/src` directory to fetch data from the Nitric API. |
| 95 | + |
| 96 | +```tsx title: main-website/src/App.tsx |
| 97 | +import { useState } from 'react' |
| 98 | +import reactLogo from './assets/react.svg' |
| 99 | +import viteLogo from '/vite.svg' |
| 100 | +import './App.css' |
| 101 | + |
| 102 | +function App() { |
| 103 | + const [count, setCount] = useState(0) |
| 104 | + |
| 105 | + const handleApiCall = async () => { |
| 106 | + try { |
| 107 | + const response = await fetch('/api/main/hello/world') |
| 108 | + if (!response.ok) { |
| 109 | + throw new Error('Network response was not ok') |
| 110 | + } |
| 111 | + const message = await response.text() |
| 112 | + alert(message) |
| 113 | + } catch (error) { |
| 114 | + console.error('There was a problem with the fetch operation:', error) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <div> |
| 121 | + <a href="https://vite.dev" target="_blank"> |
| 122 | + <img src={viteLogo} className="logo" alt="Vite logo" /> |
| 123 | + </a> |
| 124 | + <a href="https://react.dev" target="_blank"> |
| 125 | + <img src={reactLogo} className="logo react" alt="React logo" /> |
| 126 | + </a> |
| 127 | + </div> |
| 128 | + <h1>Vite + React</h1> |
| 129 | + <div className="card"> |
| 130 | + <button onClick={() => setCount((count) => count + 1)}> |
| 131 | + count is {count} |
| 132 | + </button> |
| 133 | + <p> |
| 134 | + Edit <code>src/App.tsx</code> and save to test HMR |
| 135 | + </p> |
| 136 | + </div> |
| 137 | + <button onClick={handleApiCall}>Fetch Data</button> |
| 138 | + <p className="read-the-docs"> |
| 139 | + Click on the Vite and React logos to learn more |
| 140 | + </p> |
| 141 | + </> |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +export default App |
| 146 | +``` |
| 147 | + |
| 148 | +## Running the project |
| 149 | + |
| 150 | +Run the following command in your project directory to start both the API and the website: |
| 151 | + |
| 152 | +```bash |
| 153 | +nitric start |
| 154 | +``` |
| 155 | + |
| 156 | +This will launch both the backend API and the frontend website. The website will be available at [localhost:5000](http://localhost:5000). |
| 157 | + |
| 158 | +## Testing the API |
| 159 | + |
| 160 | +You can also preview your website from within the Nitric dashboard, open the local dashboard at <a target="_blank" href="http://localhost:49152">localhost:49152</a>, then navigate to the Websites tab. |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +Click the **Fetch Data** button in the UI to call the API. If successful, you should see an alert displaying the message **"Hello world"**. |
| 165 | + |
| 166 | +## Deploying to AWS |
| 167 | + |
| 168 | +### Create your stack |
| 169 | + |
| 170 | +Create an AWS stack called `aws-staging` for your staging environment. |
| 171 | + |
| 172 | +```bash |
| 173 | +nitric stack new dev aws |
| 174 | +``` |
| 175 | + |
| 176 | +Inside the stack file, ensure you set your `region`. |
| 177 | + |
| 178 | +```yaml title:nitric.dev.yaml |
| 179 | +provider: nitric/aws@latest |
| 180 | +region: us-east-2 |
| 181 | +``` |
| 182 | +
|
| 183 | +### Deploy |
| 184 | +
|
| 185 | +Deploy to AWS using the `nitric up` command. Ensure you have set up your [AWS credentials](/providers/pulumi/aws#usage) correctly. |
| 186 | + |
| 187 | +```bash |
| 188 | +nitric up |
| 189 | +``` |
| 190 | + |
| 191 | +### Tear down |
| 192 | + |
| 193 | +To avoid unwanted costs of running your test app, you can tear down the stack using the `nitric down` command. |
| 194 | + |
| 195 | +```bash |
| 196 | +nitric down |
| 197 | +``` |
| 198 | + |
| 199 | +### What's next? |
| 200 | + |
| 201 | +You now have a basic Website and API all wired up together, use this as a basis for your next app idea! |
0 commit comments