Last active
February 27, 2023 14:44
-
-
Save marvinkome/63b6836fcbb8072bf77c98beffd1e50e to your computer and use it in GitHub Desktop.
NextAPI - Template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const LOG_TAG = ""; | |
| export default function handle(req, res) { | |
| try { | |
| const { method, body, query } = req; | |
| switch (method) { | |
| case "GET": { | |
| return res.send({ message: "hello world" }); | |
| } | |
| default: | |
| console.warn("%s unauthorized method - %s", LOG_TAG, method); | |
| return res.status(500).send({ error: "unauthorized method" }); | |
| } | |
| } catch (error) { | |
| console.error("%s general error - %j", LOG_TAG, { | |
| name: error.name, | |
| message: error.message, | |
| stack: error.stack, | |
| }); | |
| return res.status(500).send({ error: "request failed" }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { NextRequest, NextResponse } from "next/server"; | |
| const LOG_TAG = ""; | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const query = request.nextUrl.searchParams; | |
| const body = await request.json(); | |
| return NextResponse.json({ message: "hello world" }); | |
| } catch (error: any) { | |
| console.error("%s general error - %j", LOG_TAG, { | |
| name: error.name, | |
| message: error.message, | |
| stack: error.stack, | |
| }); | |
| return NextResponse.json({ error: "request failed" }, { status: 500 }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment