In questo articolo vedremo come impostare gli header HTTP in ExpressJS.
Dalla versione 4 in poi è possibile utilizzare il metodo .set(field, [value])
dell'oggetto response
:
'use strict';
app.get('/sitemap.xml', (req, res) => {
res.set('Content-Type', 'text/xml');
});
Se si vogliono impostare più header è possibile passare al metodo un oggetto letterale:
'use strict';
const fs = require('fs');
const mapInfo = fs.statSync('sitemap.xml');
app.get('/sitemap.xml', (req, res) => {
res.set({
'Content-Type' : 'text/xml',
'Content-Length': mapInfo.size.toString()
});
});
In alternativa è possibile usare l'alias res.header(field, [value])
.