Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 655ca12

Browse files
add oak guide
1 parent 6dcb11a commit 655ca12

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

docs/guides/nodejs/oak.mdx

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
description: 'How to build an Oak application with Nitric'
3+
tags:
4+
- API
5+
- HTTP Proxy
6+
languages:
7+
- typescript
8+
- javascript
9+
published_at: 2025-01-15
10+
updated_at: 2025-01-15
11+
---
12+
13+
# Enhance Oak Apps with Cloud Resources
14+
15+
In this guide we'll be scaffolding a new [Oak](https://oakserver.org/) application and including the Nitric framework to allow for the addition of other cloud resources like topics, queues and buckets.
16+
17+
## Prerequisites
18+
19+
To complete this guide you'll need the following:
20+
21+
- [Deno](https://deno.com/) installed locally
22+
- [Nitric CLI](/get-started/installation) installed
23+
- _(optional)_ Your choice of an [AWS](https://aws.amazon.com), [GCP](https://cloud.google.com) or [Azure](https://azure.microsoft.com) account
24+
25+
## Getting Started
26+
27+
Let's start by setting up a Nitric project:
28+
29+
```bash
30+
nitric new oak-example js-starter
31+
```
32+
33+
Then install dependencies and add Oak:
34+
35+
```bash
36+
cd oak-example
37+
deno install
38+
deno add "jsr:@oak/oak"
39+
```
40+
41+
You can go ahead and open this new project in your editor of choice. You should see a project structure similar to:
42+
43+
```txt
44+
├── services
45+
│ ├── api.ts
46+
├── node_modules
47+
│ ├── ...
48+
├── .gitignore
49+
├── deno.dockerfile
50+
├── deno.dockerfile.dockerignore
51+
├── deno.json
52+
└── deno.lock
53+
├── nitric.yaml
54+
├── README.md
55+
```
56+
57+
In this structure you'll notice the `services` folder. By default, this is where Nitric expects the entrypoint code for your application. However, that's just a convention, we can change that to anything else that suits our needs.
58+
59+
Now, let's add some oak code to get things started.
60+
61+
```typescript title:services/api.ts
62+
import { Application, Router } from 'jsr:@oak/oak'
63+
64+
const app = new Application()
65+
const router = new Router()
66+
67+
app.use(router.routes())
68+
app.use(router.allowedMethods())
69+
```
70+
71+
This code sets up our application and a router to start defining routes. We can define a route that accesses a Nitric bucket and returns an upload ID.
72+
73+
```typescript title:services/api.ts
74+
import { http, bucket } from 'npm:@nitric/sdk'
75+
import { Application, Router } from 'jsr:@oak/oak'
76+
77+
export const imgBucket = bucket('images').allow('read', 'write')
78+
79+
const app = new Application()
80+
const router = new Router()
81+
82+
app.use(router.routes())
83+
app.use(router.allowedMethods())
84+
85+
router.get('/upload/:id', async (ctx) => {
86+
try {
87+
const img = imgBucket.file(`images/${ctx.params.id}/photo.png`)
88+
89+
ctx.response.type = 'application/json'
90+
ctx.response.body = { url: await img.getUploadUrl() }
91+
} catch (_err) {
92+
ctx.response.status = 500
93+
ctx.response.body = 'Error getting file URL'
94+
}
95+
})
96+
```
97+
98+
Finally, you'll notice that we imported the `http` resource in the previous example. This will be used to wrap our application so the Nitric server can proxy requests. Add the following code to the bottom of your file:
99+
100+
```typescript title:services/api.ts
101+
async function bootstrap(port: number) {
102+
await app.listen({ port })
103+
104+
return {
105+
on: app.addEventListener,
106+
}
107+
}
108+
109+
http(bootstrap)
110+
```
111+
112+
This bootstrap function is called when our service is run. It will forward the port, which is generated by the Nitric server, to the Oak application. It will then return a "Server" object so that Nitric can handle some of the lifecycle events that happen under the hood, like errors and closes. At this point, we're ready to start testing locally.
113+
114+
<Note>
115+
The application port will be set by the NITRIC_HTTP_PROXY_PORT environment
116+
variable, however it will find an open port if that environment variable isn't
117+
set.
118+
</Note>
119+
120+
```bash
121+
nitric start
122+
```
123+
124+
Your oak application will now be running with Nitric acting as a proxy. We can test this in another terminal or web browser.
125+
126+
```bash
127+
curl http://localhost:4001/upload/a1b2c3d4e5
128+
{ "url":"http://localhost:51714/write/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" }
129+
```
130+
131+
At this point, we can stop the running application and try to deploy it to a cloud provider.
132+
133+
## Deploy to the cloud
134+
135+
This is where the true value of Nitric shines. You don't need to perform any manual cloud deployment activities or add solutions like Terraform to get this project into your cloud environment, Nitric takes care of that for you.
136+
137+
To perform the deployment we'll create a `stack`, stacks give Nitric the configuration needed for a specific cloud instance of this project, such as the provider and region.
138+
139+
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
140+
141+
```bash
142+
nitric stack new dev aws
143+
```
144+
145+
Edit the stack file `nitric.dev.yaml` and set your preferred AWS region, for example `us-east-1`.
146+
147+
```yaml title:nitric.dev.yaml
148+
# The nitric provider to use
149+
provider: nitric/aws@latest
150+
# The target AWS region to deploy to
151+
# See available regions:
152+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
153+
region: us-east-2
154+
```
155+
156+
<Note>
157+
You are responsible for staying within the limits of the free tier or any
158+
costs associated with deployment.
159+
</Note>
160+
161+
Let's try deploying the stack with the `up` command:
162+
163+
```bash
164+
nitric up
165+
```
166+
167+
When the deployment is complete, go to the relevant cloud console and you'll be able to see and interact with your WebSocket application.
168+
169+
To tear down your application from the cloud, use the `down` command:
170+
171+
```bash
172+
nitric down
173+
```

0 commit comments

Comments
 (0)