원두마켓
Next.js에서 mongoose 연동해서 API 만들기
Next.js에서 API 만들기 Next.js에서 pages/api 디렉토리에서 파일/디렉토리 이름으로 API endpoint를 나타낼 수 있다. pages/api/user.ts export default function handler(req, res) { if (req.method === 'GET') { // GET 요청 처리 } else if (req.method === 'POST') { // POST 요청 처리 } } /user로 들어온 요청이 pages/api/user.ts에서 처리된다. next-connect 라이브러리를 사용하면 마치 Express.js에서처럼 미들웨어의 형태로 코드를 작성할 수 있다. import { NextApiRequest, NextApiResponse } from 'ne..