Express res.append() Method
What you’ll learn
- When to use
res.append()for repeated header values. - How it differs from
res.set(). - How to append arrays and single values correctly.
- Common ordering mistakes and how to avoid them.
Syntax
javascript
res.append(field, value)
res.append('Link', ['</css/main.css>; rel=preload', '</js/app.js>; rel=preload'])1
Append another Link header value
javascript
app.get('/preload', function (req, res) {
res.append('Link', '</styles/site.css>; rel=preload; as=style');
res.append('Link', '</scripts/app.js>; rel=preload; as=script');
res.send('Headers appended');
});2
Append multiple values with an array
javascript
app.get('/vary-demo', function (req, res) {
res.append('Vary', ['Accept-Encoding', 'Accept-Language']);
res.send('Vary header updated');
});3
Order matters with set and append
javascript
app.get('/order', function (req, res) {
res.append('Warning', '199 - "old value"');
res.set('Warning', '299 - "new value"'); // replaces earlier header
res.send('Check Warning header');
});⚠️ Common pitfalls
- Calling
res.set()afterres.append()can overwrite previously appended values. - Do not append headers after response is already sent.
- Use correct header names and valid values to avoid client/proxy parsing issues.
❓ FAQ
It appends one or more values to an HTTP response header without replacing the existing values.
res.set() replaces the header value, while res.append() adds another value to the same header.
Yes. Express appends each value in the array for that header.
Yes, it is commonly used when sending multiple Set-Cookie values in one response.
A later res.set() call can overwrite the header, so order matters.
Did you know?
res.append() adds a new value to an existing response header instead of replacing it.
4 people found this page helpful
