douye4051 2016-01-07 20:07
浏览 231
已采纳

如何在Express JS中使用多个Http Header(同名)

My Php Code can be send two headers with same name, in php second parameter is for replace or override. but in ExpressJS res.header is not like php code PHP Code:

header("Link: <http://$prefetch_next_img>; rel=prefetch",false); 
header("Link: <http://$prefetch_next_img2>; rel=prefetch",false);  

http://us2.php.net/manual/en/function.header.php

ExpressJS(NodeJS) :

res.header('Link','Fake Value');
res.header('Link','Only Send it'); // previous header replaced
  • 写回答

1条回答 默认 最新

  • douchong4730 2016-01-07 21:04
    关注

    Ultimately, you'll need to pass an array to set multiple values for the same header

    res.header('Set-Cookie', ['foo', 'bar']);
    

    Looking at response library for express we can see the following examples:

    /**
     * Set header `field` to `val`, or pass
     * an object of header fields.
     *
     * Examples:
     *
     *    res.set('Foo', ['bar', 'baz']);
     *    res.set('Accept', 'application/json');
     *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
     *
     * Aliased as `res.header()`.
     *
     * @param {String|Object} field
     * @param {String|Array} val
     * @return {ServerResponse} for chaining
     * @public
     */
    

    A deeper dive into the code shows us that this is really just a proxy for node's response.setHeader(), which follows the same format

    Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here if you need to send multiple headers with the same name.

    response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序