2013-02-18 24 views
16

Yanıt nasıl ayarlanır? Location Express'te HTTP üstbilgisi nasıl ayarlanır? Bunu denedim ama işe yaramıyor:Express framework'te konum yanıtı HTTP üstbilgisi nasıl ayarlanır?

Controller.prototype.create = function(prop, res) { 
    var inst = new this.model(prop); 

    inst.save(function(err) { 
     if (err) { 
     res.send(500, err); 
     } 

     res.location = '/customers/' + inst._id; 
     res.send(201, null); 
    }); 
    }; 

Bu kod MongoDB içine yeni bir belge sürüp gidiyor ve rekabet üzerine konumunu belirleyen ve bir 201 yanıt göndermek. Got bu yanıtı, hayır Location başlık ayarlanır:

HTTP/1.1 201 Created 
X-Powered-By: Express 
Content-Length: 0 
Date: Mon, 18 Feb 2013 19:08:41 GMT 
Connection: keep-alive 

DÜZENLEME: (serin nick btw) cevap, res.setHeader eserler ǝpısdn uʍop olarak. Ama neden res.location yapmıyor?

cevap

24

res.location ayarlıyorsunuz. res.location bir işlevdir.

res.location('/customers/' + inst._id) 
15

res nesne setHeader() ortaya:

res.setHeader('Location', foo); 

yerine res.location bu deneyin.

+0

Çalışıyor. Ama neden res.location' (http://expressjs.com/api.html#res.location dokümantasyonuna göre) yazmıyor? – gremo

+1

'konumu' 'özellik' değil, bir yöntemdir. @Johnathan Ong'un cevabı. – slezica

+1

Konum için özel olarak, diğer yorumculardan dediği gibi res.location() yöntemini kullanabilirsiniz. Ancak diğer başlıklar veya özel başlıklar için res.setHeader() değil res.set(). –

İlgili konular