import { NextApiRequest, NextApiResponse } from "next"; import Message from "../models/messages"; } export const config = { api: { externalResolver: true } } export const createMessage = async ( req: NextApiRequest, res: NextApiResponse ) => { const { messageBody } = req.body; try { const message = await Message.create({ messageBody, label: "Human" }); res.status(200).json(message); res.end() } catch (error) { res.status(500).json(error); } }; export const getMessages=async( req: NextApiRequest, res: NextApiResponse)=>{ try { const messages= await Message.find({}) res.status(200).json(messages); res.end() } catch (error) { res.json(error) } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter