Enviando una respuesta de Koa

La traducción del artículo se preparó antes del inicio del curso para desarrolladores de Node.js






Koa es un pequeño marco que le permite crear aplicaciones de backend que se ejecutan en Node.js



.





En este artículo, veremos cómo enviar diferentes tipos de respuestas usando Koa.





Despacho del cuerpo

Puede configurar el atributo body para enviar el cuerpo de la respuesta ctx



. Por ejemplo, podemos enviar el cuerpo de la respuesta así:





const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {  
  ctx.body = 'foo';
});
app.listen(3000);
      
      



ctx.body



'foo' . , , , /



HTTP-.





(Header)

Koa, ctx.response



. ctx.set



. , :





const app = new Koa();
app.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  ctx.set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
  ctx.body = 'hello';
});
app.listen(3000);
      
      



ctx.set



, : Access-Control-Allow-Origin



, Access-Control-Allow-Headers



Access-Control-Allow-Methods



.





/



, HTTP , Chrome Postman.





, respondbse.status



.





, :





const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
  ctx.status = 202;
  ctx.body = 'accepted';
});
app.listen(3000);
      
      



ctx.status 's 202. , / , 202 .





, , :





200 - OK







201 - created -







202 - accepted -







203 - non-authoritative information-







204 -  no content-







301 - moved permanently -







302 - found -







303 - see other - .







307 - temporary redirect -







308 - permanent redirect -







400 -  bad request -







401 -  unauthorized -







403 - forbidden-







404 - not found -







405 - method not allowed-







406 - not acceptable-







422 - unprocessable entity -







500 - internal server error-







501 - not implemented-







502 - bad gateway-







504 - gateway timeout-







(Headers)

ctx.response.lastModified



.





, :





const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
  ctx.response.lastModified = new Date(2020, 0, 1);
  ctx.body = 'foo';
});
app.listen(3000);
      
      



lastModified



1 2020 , , /



, Last-Modified



Wed, 01 2020 00:00:00 GMT



.





Content-Type, ctx.type



. , :





const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
  ctx.type = 'text/plain; charset=utf-8';
  ctx.body = 'foo';
});
app.listen(3000);
      
      



:





ctx.type = 'text/plain; charset=utf-8';
      
      



Content-Type 'text/plain; charset=utf-8'



. Content-Type /



.





, ctx.append()



. 2 .





, :





const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
  ctx.append('a', 1);
  ctx.body = 'foo';
});
app.listen(3000);
      
      



ctx.append()



'a'



1.





, /



, A 1.





, ctx.append()



. , ctx.body



.





, ctx.status



.

















All Articles