Skip to content

MrZhouZh/next-restful-api

Repository files navigation

This is a Next.js project bootstrapped with create-next-app.

Getting Started

First, run the development server:

npm run dev
# or
yarn dev
# or
pnpm dev

Open http:https://localhost:3000 with your browser to see the result.

You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.

This project uses next/font to automatically optimize and load Inter, a custom Google Font.

TODO

  • gif sharp compression

API

GET /api/todos

POST /api/todos

PUT /api/todos

DELETE /api/todos

curl --request GET \
     --url 'http:https://localhost:3000/api/todos' \
     --header 'User-Agent: Thunder Client (https://www.thunderclient.com)' \
     --header 'accept: application/json'

Learn More

To learn more about Next.js, take a look at the following resources:

You can check out the Next.js GitHub repository - your feedback and contributions are welcome!

Deploy on Vercel

Notes: Please set some enviorment before deploy. Vercel environment-variables. see the .env.example file

screenshot

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

📝 Additions Info

Thunder Client - vscode extension 轻量级测试接口扩展

typicode - Todo Api Mock server

Get Repo Trees - Github API

Issues

Some problems from development to deployment

  1. pnpm run build cause a error:
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11457:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: connect ECONNREFUSED ::1:3000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -61,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 3000
  }
}

Error occurred prerendering page "/todos". Read more: https://nextjs.org/docs/messages/prerender-error

Resolved:

reference: "Fetch failed" error in production mode

// src/app/todos/page.tsx
export const runtime = "edge"
  1. After deployed process.env.[xxx] is undefined

Resolved:

// next.config.js
module.exports = {
+   env: {
+     DATA_API_KEY: process.env.DATA_API_KEY,
+     API_URL: process.env.API_URL,
+     GITHUB_TOKEN: process.env.GITHUB_TOKEN,
+   },
}

Vercel should also set some enviroment variables

go to set! ➡️

  1. fetch api interface cause CROSS-ORIGIN error:

reference: https://blog.logrocket.com/using-cors-next-js-handle-cross-origin-requests/

Resolved:

# More details see: src/middleware.ts
export function middleware(req: Request) {
  ...
+  const res = NextResponse.next()
+  if (origin && allowOrigins.includes(origin)) {
+    res.headers.appendif (origin && allowOrigins.includes(origin)) {
+    res.headers.append('Access-Control-Allow-Origin', origin);
+    // res.headers.append('Access-Control-Allow-Origin', '*') // replace this your actual origin
+  }
+  res.headers.append('Access-Control-Allow-Credentials', "true")
+  res.headers.append('Access-Control-Allow-Methods', 'GET,DELETE,PATCH,POST,PUT')
+  res.headers.append(
+    'Access-Control-Allow-Headers',
+    'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
+  )
+
+  return res
}

Redeploy.