This is a Next.js project bootstrapped with create-next-app
.
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.
- gif sharp compression
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'
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
Notes: Please set some enviorment before deploy. Vercel environment-variables. see the
.env.example
file
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.
Thunder Client - vscode extension 轻量级测试接口扩展
typicode - Todo Api Mock server
Some problems from development to deployment
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"
- After deployed
process.env.[xxx]
isundefined
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
- 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.