Node.js

4.1.1.2. Express – Response Nesnesinin Özellikleri ve Metotları

res nesnesi HTTP isteğine karşı bir Express uygulaması tarafından gönderilen HTTP cevabı temsil eder.

Response Nesnesinin Özellikleri

  • res.app – Bu özellik ara katman kullanan express uygulamasının örneğine bir referans tutar.
  • req.headerSent – Eğer uygulama cevap için HTTP header gönderirse bu bilgi Boolean bir özellikte gösterilir.
  • req.locals – İsteğe cevap yerel değişkenler içeren bir nesne.

Response Nesnesi Metotları

res.append(field [, value])

Bu metot, HTTP response header alanına belirli bir değer ekler.

res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
res.append('Uyari', '199 Miscellaneous uyarisi');

res.attachment([filename])

Bu metot HTTP cevabında ek bir dosya olarak göndermek için kullanılır.

res.attachment('path/to/logo.png');

res.cookie(name, value [, options])

Bu metot cookie name’nin değerine veri göndermek için kullanılır. Bu değerin parametresi bir metin veya JSON formatına çevrilmiş bir nesne olabilir. Örneğin:

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

res.cookie('cart', { items: [1,2,3] });
res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });

res.clearCookie(name [, options])

Bu metot belirli bir isme sahip cookie’yi temizlemek için kullanılır.

res.cookie('name', 'tobi', { path: '/admin' });
res.clearCookie('name', { path: '/admin' });

res.download(path [, filename] [, fn])

Bu metot, dosyadaki yolu bir “ek” olarak transfer etmek için kullanılır. Tipik olarak, tarayıcılar kullanıcıya indirme isteğinde bulunur.

res.download('/report-12345.pdf');

res.download('/report-12345.pdf', 'report.pdf');

res.download('/report-12345.pdf', 'report.pdf', function(err){
});

res.end([data] [, encoding])

Bu metot yanıtı sonlandırmak için kullanılır.

res.end();

res.status(404).end();

res.format(object)

Bu metot, varsa istek nesnesininde Accept HTTP header konusunda içerik aktarma için kullanılır.

res.format({
   'text/plain': function(){
      res.send('hey');
   },

   'text/html': function(){
      res.send('hey');
   },

   'application/json': function(){
      res.send({ message: 'hey' });
   },

   'default': function() {
      // İstemi logla ve 406 ile cevapla
      res.status(406).send('Kabul edilemez');
   }
});

res.get(field)

Bu yöntem, alan tarafından belirtilen HTTP response üstbilgisini döndürmek için kullanılır.

res.get('Content-Type');

res.json([body])

Bu metot bir JSON cevabı göndermek için kullanılır.

res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })

res.jsonp([body])

Bu metot JSONP (JSON with padding) desteğiyle bir JSON cevabı göndermek için kullanılır. JSONP, istemciden farklı bir alanda bulunan bir sunucudaki verileri istemek için kullanılır.

res.jsonp(null)
res.jsonp({ user: 'tobi' })
res.status(500).jsonp({ error: 'message' })

res.links(links)

Bu metot yanıtın Link HTTP başlık alanı doldurmak için parametre özellikleri olarak sağlanan bağlantılara katılmak için kullanılır.

res.links({
   next: 'http://api.example.com/users?page=2',
   last: 'http://api.example.com/users?page=5'
});

res.location(path)

Bu yöntem, belirlenen yol parametresini temel alan Location HTTP header alanı cevap belirlemek için kullanılır.

res.location('/foo/bar');
res.location('foo/bar');
res.location('http://example.com');

res.redirect([status,] path)

Bu metot belirlenen HTTP status kodu ile belirlenen bir yoldan türetilmiş URL’yi yönlendirmek için kullanılır.

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');

res.render(view [, locals] [, callback])

Bu yöntem bir görünümü (view) oluşturmak için kullanılır ve oluşturulan HTML metnini istemciye gönderir.

// istemciye uretilen view'i gonderir
res.render('index');

// view'e yerel degiskenleri aktarir
res.render('user', { name: 'Tobi' }, function(err, html) {
   // ...
});

res.send([body])

Bu yöntem, HTTP cevabını göndermek için kullanılır.

res.send(new Buffer('whoop'));
res.send({ some: 'json' });
res.send('
some html');

res.sendFile(path [, options] [, fn])

Bu yöntem dosyayı verilen yoldan aktarmak için kullanılır. dosya adının uzantısına dayandırarak Content-Type cevabı HTTP header alanını ayarlar.

res.sendFile(fileName, options, function (err) {
   // ...
});

res.sendStatus(statusCode)

Bu metot, cevap içeriğini HTTP status kodunu statusCode ayarlamak ve kendi metin görünümünü ayarlamak için kullanılır.

res.sendStatus(200); // Esittir: res.status(200).send('OK')
res.sendStatus(403); // Esittir: res.status(403).send('Forbidden')
res.sendStatus(404); // Esittir: res.status(404).send('Not Found')
res.sendStatus(500); // Esittir: res.status(500).send('Internal Server Error')

res.set(field [, value])

Bu metot, HTTP header alanının cevabının değerini ayarlamak için kullanılır.

res.set('Content-Type', 'text/plain');

res.set({
   'Content-Type': 'text/plain',
   'Content-Length': '123',
   'ETag': '12345'
})

res.status(code)

Bu metot, cevap için HTTP statüsü ayarlamak için kullanılır.

res.status(403).end();
res.status(400).send('Bad Request');
res.status(404).sendFile('/absolute/path/to/404.png');

res.type(type)

Bu metot, Content-Type HTTP header’a MIME type ayarlamak için kullanılır.

res.type('.html');              // => 'text/html'
res.type('html');               // => 'text/html'
res.type('json');               // => 'application/json'
res.type('application/json');   // => 'application/json'
res.type('png');                // => image/png: