Skip to content

Commit 7299b3f

Browse files
feature: add new endpoint /api/posts
1 parent 1069bd9 commit 7299b3f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pages/api/posts.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NextApiRequest, NextApiResponse } from 'next'
2+
3+
module.exports = async (req: NextApiRequest, res: NextApiResponse) => {
4+
if (req.method === 'GET') {
5+
const posts = [
6+
{
7+
id: 1,
8+
title: 'React',
9+
body: 'React is the JavaScript library for building UIs.',
10+
date: '2020-01-01',
11+
},
12+
{
13+
id: 2,
14+
title: 'Next.js',
15+
body: 'Next.js is the framework for React developers.',
16+
date: '2020-02-01',
17+
},
18+
]
19+
20+
res.status(200).json({ posts })
21+
res.end()
22+
} else {
23+
res.end(`Invalid method ${req.method}. Only GET is allowed.`)
24+
}
25+
}

0 commit comments

Comments
 (0)