English 中文(简体)
• 如何在14个小区确定一条皮路线?
原标题:How do i define an api route in next js 14?
  • 时间:2023-11-27 17:22:13
  •  标签:
  • rest
  • next.js

I m 试图建造一个简单的REST管道。 我有一条路线,在弧/申请/申请/笔/时间夹中存档。 我的路线是:

import { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
  message: string
}
 
export function GET(
  req: NextApiRequest,
  res: NextApiResponse<ResponseData>
) {
  res.status(200).json({ message:  Hello from Next.js!  })
}

因此,它从国际契约中仅举一个基本例子。 当我打电话到地方厅时:3000/api/timetable I m 获取类型Error:Res.status不是功能。

使用直线器的Im, 字面为5.1.3, 下面的j为14.0.3。

我试图将其放在文件夹(如网页路由器)上,但却没有帮助。

最佳回答

将定义改为

export function GET(
  req: NextApiRequest,
) {
  return Response.json({ message:  Hello from Next.js!  })
}

方法定义没有反应。

问题回答

我使用这个词是玩笑的,是行之有效的。

export function GET (req,res){
    Response.json({Msg:"your message"}, {status:200});
}

第14条不使用<条码>进口{下阿皮雷问,下ApiResponse }从“next”中删除;

你们能够照此办理。

//app/api/test/route.js

import { NextResponse } from  next/server 
 
export async function GET(request: Request) {
  return NextResponse.json({ msg:  Hello from server  })
}

相信这一点,你就能够从这里带走。

此处应充分参考网站的说明。

https://nextjs.org/docs/app/building-your-application/routing/route-handlers

https://nextjs.org/docs/app/api-fer/Functions/next-response#json/a>

希望这一帮助

1. 翻番





相关问题
Allow RESTful DELETE method in asp.net mvc?

im currently setting up asp.net to accept DELETE http verb in the application. However, when i send "DELETE /posts/delete/1" i always get a 405 Method not allow error. I tried to take a look at ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

Use HTTPClient or HttpUrlConnection? [closed]

We re implementing a REST client on JRE 1.4. Seems two good options for a client REST framework are HttpClient and HttpUrlConnection. Is there a reason to use HttpClient over the JRE s ...

Why can t I find the truststore for an SSL handshake?

I m using the Spring RESTTemplate on the client side to make calls to a REST endpoint. The client in this case is a Spring app and Tomcat is the servlet container. I m running into issues making a ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Three Step Buyonline The RESTful way

We are re-developing our buyonline functionality and we are doing it the RESTful way. The process is a three step one and the customer is asked to enter data at each step. Let s say the three URL s ...

热门标签