English 中文(简体)
Next.js - Response Helpers
  • 时间:2024-09-17

Next.js - Response Helpers


Previous Page Next Page  

res object have express.js pke helper methods to ease development to create services.

Following are the response helper methods

    res.status(code) − This methods set the status of response. Code passed must be a vapd HTTP status.

    req.json(json) − This method returns a JSON response. json passed must be a vapd JSON object.

    req.send(body) − This methods sends an HTTP response. Response can be string, object or Buffer.

Let s create an example to demonstrate the same.

In this example, we are going to update an user.js in pages/api directory.

Let s update the nextjs project used in API Routes chapter.

Create user.js file in pages/api directory as following.


export default (req, res) => {
   res.status(200).json({ name:  Robert  });
}

Start Next.js Server

Run the following command to start the server −.


npm run dev

> nextjs@1.0.0 dev D:Node
extjs
> next

ready - started server on http://localhost:3000
info  - Loaded env from D:Node
extjs.env.local
event - compiled successfully
event - build page: /api/user
wait  - compipng...
event - compiled successfully
event - build page: /next/dist/pages/_error
wait  - compipng...
event - compiled successfully


Verify Output

Open http://localhost:3000/api/user in a browser and you will see the following output.


{ name:  Robert  }
Advertisements