Skip to content

Instantly share code, notes, and snippets.

@skolhustick
Created September 3, 2022 14:27
Show Gist options
  • Save skolhustick/bf59cd68fd2814cf85a685b0b5833737 to your computer and use it in GitHub Desktop.
Save skolhustick/bf59cd68fd2814cf85a685b0b5833737 to your computer and use it in GitHub Desktop.
astro-mongodob-api.users.js
// src/pages/api/users.js
import { createUser, getAllUsers } from "../../lib/users";
export const get = async () => {
const users = await getAllUsers();
if (!users) {
return new Response(null, {
status: 404,
statusText: "Not found",
});
}
return new Response(JSON.stringify(users), {
status: 200,
});
};
export const post = async ({ request }) => {
const newUser = await request.json();
const user = await createUser(newUser);
return new Response(JSON.stringify(user), {
status: 200,
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment