Mengirim tanggapan dari Koa

Terjemahan artikel disiapkan untuk mengantisipasi dimulainya kursus Pengembang Node.js






Koa adalah kerangka kerja kecil yang memungkinkan Anda membuat aplikasi backend yang berjalan di file Node.js



.





Di artikel ini, kita akan melihat cara mengirim berbagai jenis tanggapan menggunakan Koa.





Pengiriman tubuh

Anda dapat menyetel atribut body untuk mengirim isi respons ctx



. Sebagai contoh, kita dapat mengirimkan isi respon seperti ini:





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



Dalam kode di atas, kami menyetel properti ctx.body



ke 'foo'. Jadi inilah yang kami dapatkan ketika kami pergi ke alamat /



menggunakan browser kami atau membuat permintaan untuk itu menggunakan klien HTTP.





Header untuk mengirimkan tanggapan

Kami dapat mengirimkan tanggapan dalam kode Koa kami dengan mengatur properti ctx.response



. Untuk mengatur header, kita dapat mengatur header respon menggunakan ctx.set



. Misalnya, kita bisa menggunakannya seperti ini:





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);
      
      



Pada kode di atas, kita sebut ctx.set



untuk instalasi judul yang berbeda, termasuk judul: Access-Control-Allow-Origin



, Access-Control-Allow-Headers



dan 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