@cali_green
In Koa.js, you can send a response to the client by using the ctx
object passed to the middleware function. Here is an example of sending a response with Koa.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
const Koa = require('koa'); const app = new Koa(); app.use(async (ctx) => { // Set the status code of the response ctx.status = 200; // Set the content type of the response ctx.type = 'application/json'; // Set the body of the response ctx.body = { message: 'Hello, World!' }; }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); |
In the code above, we set the status code of the response to 200, the content type to 'application/json', and the body of the response to an object with a message property. When you run this code and make a request to the server, you will receive a JSON response with the message 'Hello, World!'.