Skip to content

Instantly share code, notes, and snippets.

@marvinkome
Last active February 27, 2023 14:44
Show Gist options
  • Select an option

  • Save marvinkome/63b6836fcbb8072bf77c98beffd1e50e to your computer and use it in GitHub Desktop.

Select an option

Save marvinkome/63b6836fcbb8072bf77c98beffd1e50e to your computer and use it in GitHub Desktop.
NextAPI - Template
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" });
}
}
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