Skip to content

Commit b36d7a3

Browse files
committed
example: move api route one level deep
1 parent e8ecc87 commit b36d7a3

File tree

3 files changed

+13
-37
lines changed

3 files changed

+13
-37
lines changed

example/README.md

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1+
# Next.js Koa api example
22

3-
## Getting Started
3+
This is a Next.js api with one exported route which uses the `KaoApi` library.
44

5-
First, run the development server:
5+
run:
66

7-
```bash
8-
npm run dev
9-
# or
10-
yarn dev
7+
```sh
8+
pnpm dev
119
```
12-
13-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14-
15-
You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.
16-
17-
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
18-
19-
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
20-
21-
## Learn More
22-
23-
To learn more about Next.js, take a look at the following resources:
24-
25-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27-
28-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29-
30-
## Deploy on Vercel
31-
32-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
33-
34-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.

example/request.http

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
GET http://localhost:3000/api/
1+
GET http://localhost:3000/api/level_one
22

33
### trigger error
4-
GET http://localhost:3000/api/throw
4+
GET http://localhost:3000/api/level_one/throw
55

66
###
7-
POST http://localhost:3000/api/hello
7+
POST http://localhost:3000/api/level_one/hello
88
Content-Type: application/json
99

1010
{
1111
"name":"Luigi"
1212
}
1313

1414
### nested router
15-
GET http://localhost:3000/api/nested/hello
15+
GET http://localhost:3000/api/level_one/nested/hello
1616
Content-Type: application/json

example/src/pages/api/[[...hello]].ts renamed to example/src/pages/api/level_one/[[...hello]].ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { KoaApi, withKoaApi } from 'nextjs-koa-api'
22

33
const api = new KoaApi({
44
router: {
5-
prefix: '/api'
5+
prefix: '/api/level_one'
66
}
77
})
88

@@ -20,11 +20,12 @@ api.use(async (ctx, next) => {
2020
})
2121

2222
api.router
23-
.get('/hello', (ctx) => {
23+
.get('/', (ctx) => {
2424
ctx.body = {
2525
method: 'GET',
26-
route: '/hello'
26+
route: '/'
2727
}
28+
ctx.cookies.set('my_cookie', 'my_cookie_value')
2829
})
2930
.post('/hello', (ctx) => {
3031
ctx.body = ctx.request.body

0 commit comments

Comments
 (0)