Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

next connect

// pages/api/hello.js
import nc from "next-connect";

const handler = nc({
  onError: (err, req, res, next) => {
    console.error(err.stack);
    res.status(500).end("Something broke!");
  },
  onNoMatch: (req, res, next) => {
    res.status(404).end("Page is not found");
  },
})
  .use(someMiddleware())
  .get((req, res) => {
    res.send("Hello world");
  })
  .post((req, res) => {
    res.json({ hello: "world" });
  })
  .put(async (req, res) => {
    res.end("async/await is also supported!");
  })
  .patch(async (req, res) => {
    throw new Error("Throws me around! Error can be caught and handled.");
  });

export default handler;
Comment

next-connect

// pages/api/hello.js
import type { NextApiRequest, NextApiResponse } from "next";
import { createRouter } from "next-connect";

// Default Req and Res are IncomingMessage and ServerResponse
// You may want to pass in NextApiRequest and NextApiResponse
const router = createRouter<NextApiRequest, NextApiResponse>();
// pages/api/hello.js
import type { NextApiRequest, NextApiResponse } from "next";
import { createRouter } from "next-connect";

// Default Req and Res are IncomingMessage and ServerResponse
// You may want to pass in NextApiRequest and NextApiResponse
const router = createRouter<NextApiRequest, NextApiResponse>();

router
  .use(async (req, res, next) => {
    const start = Date.now();
    await next(); // call next in chain
    const end = Date.now();
    console.log(`Request took ${end - start}ms`);
  })
  .use(authMiddleware)
  .get((req, res) => {
    res.send("Hello world");
  })
  .post(async (req, res) => {
    // use async/await
    const user = await insertUser(req.body.user);
    res.json({ user });
  })
  .put(
    async (req, res, next) => {
      // You may want to pass in NextApiRequest & { isLoggedIn: true }
      // in createRouter generics to define this extra property
      if (!req.isLoggedIn) throw new Error("thrown stuff will be caught");
      // go to the next in chain
      return next();
    },
    async (req, res) => {
      const user = await updateUser(req.body.user);
      res.json({ user });
    }
  );

// create a handler from router with custom
// onError and onNoMatch
export default router.handler({
  onError: (err, req, res) => {
    console.error(err.stack);
    res.status(500).end("Something broke!");
  },
  onNoMatch: (req, res) => {
    res.status(404).end("Page is not found");
  },
});

router
  .use(async (req, res, next) => {
    const start = Date.now();
    await next(); // call next in chain
    const end = Date.now();
    console.log(`Request took ${end - start}ms`);
  })
  .use(authMiddleware)
  .get((req, res) => {
    res.send("Hello world");
  })
  .post(async (req, res) => {
    // use async/await
    const user = await insertUser(req.body.user);
    res.json({ user });
  })
  .put(
    async (req, res, next) => {
      // You may want to pass in NextApiRequest & { isLoggedIn: true }
      // in createRouter generics to define this extra property
      if (!req.isLoggedIn) throw new Error("thrown stuff will be caught");
      // go to the next in chain
      return next();
    },
    async (req, res) => {
      const user = await updateUser(req.body.user);
      res.json({ user });
    }
  );

// create a handler from router with custom
// onError and onNoMatch
export default router.handler({
  onError: (err, req, res) => {
    console.error(err.stack);
    res.status(500).end("Something broke!");
  },
  onNoMatch: (req, res) => {
    res.status(404).end("Page is not found");
  },
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native make safe view in mobile 
Javascript :: feet to cm javascript 
Javascript :: Javascript make alert box 
Javascript :: js loop 
Javascript :: fibonacci recursion 
Javascript :: closure and scope javascript 
Javascript :: react-bootstrap problem-install new version 
Javascript :: js array index 
Javascript :: mongoose find multiple conditions 
Javascript :: change image onclick js 
Javascript :: local reload go to top 
Javascript :: node isfile or isdirectory 
Javascript :: abrir dialog angular materia 
Javascript :: javascript create an array 
Javascript :: how to add all values of array together js 
Javascript :: vue electron name and icon 
Javascript :: vim go back word 
Javascript :: js check collision 
Javascript :: framer motion reactjs 
Javascript :: dropzone react view photo 
Javascript :: javascript sleep 3 second 
Javascript :: javascript on enter key searchbox 
Javascript :: javascript select from array where 
Javascript :: tailwincss in react native 
Javascript :: javascript console.log 
Javascript :: used to retrieve dat from firebase realtime datastore 
Javascript :: add an object to an array mongosse 
Javascript :: getJSON how to set async to false 
Javascript :: Routes in react-router-dom@6 and take the path by useLocation() hook 
Javascript :: get location from brwoser react 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =