response.writeHead(statusCode[, statusMessage][, headers])

查看英文版

版本历史

发送一个响应头给请求。 状态码是一个三位数的 HTTP 状态码,如 404。 最后一个参数 headers 是响应头。 第二个参数 statusMessage 是可选的状态描述。

例子:

const body = 'hello world';
response.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain' });

该方法在消息中只能被调用一次,且必须在 response.end() 被调用之前调用。

如果在调用该方法之前调用 response.write() 或 response.end(),则隐式的响应头会被处理并调用该函数。

response.setHeader() 设置的响应头会与 response.writeHead() 设置的响应头合并,且 response.writeHead() 的优先。

// 返回 content-type = text/plain
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
res.setHeader('X-Foo', 'bar');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('ok');
});

注意,Content-Length 是以字节(而不是字符)为单位的。 上面的例子行得通是因为字符串 'hello world' 只包含单字节字符。 如果响应主体包含高级编码的字符,则应使用 Buffer.byteLength()来确定在给定编码中的字节数。 Node.js 不会检查 Content-Length 与已发送的响应主体的长度是否相同。

如果响应头字段的名称或值包含无效字符,则抛出 TypeError 错误。

response.writeHead的更多相关文章

  1. 记录nodejs的writeHead

    使用response.writeHead()时,如果第二个参数的值使用错误的字符时,会使整个页面被镶嵌在<pre></pre>中被传输过去. 例: response.write ...

  2. response.write

    response.write(chunk[, encoding][, callback])# 查看英文版 chunk <string> | <Buffer> encoding  ...

  3. nodejs -- fs模块 ---> readFile 函数 1) fs.readFile(filename, "binary", function(error, file) 2) response.write(file, "binary");

    一:代码: 1.1 入口文件: index.js var server = require('./server'); var router = require("./router" ...

  4. 如何使用IntelliJ集成nodejs进行接口测试<response demo>

    1.相关的配置及安装,在如下地址进行参考,这位大师写得也挺好(mac和windows都可以下载) https://www.jianshu.com/p/dd2d2d0ff133 2.在集成nodejs的 ...

  5. fs.createReadStream(filepath).pipe(response);这句是什么意思?

    'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...

  6. node——request和response的常用对象

    request(http.IncomingMessage)和response(http.ServerResponse)对象介绍 request:服务器解析用户提交的http请求报文,将结果解析到req ...

  7. node.js入门学习(二)MIME模块,request和response对象,demo之不同url请求不同html页面,页面包含图片、样式css等静态资源

    一.构建http服务程序-根据不同请求做出不同响应 // 加载http模块 var http = require("http"); // 创建一个http服务对象 http.cre ...

  8. 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)

    问题描述 在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store) 效果类似于本地 ...

  9. nodejs进阶(4)—读取图片到页面

    我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...

随机推荐

  1. aarch64的架构:unrecognized command line option '-mfpu=neon'

    不用添加这个'-mfpu=neon'的编译器选项了,因为这个架构下neon是默认启动的. 参考: https://lists.linaro.org/pipermail/linaro-toolchain ...

  2. layui 表格内容显示更改

    在cole 中使用temple 属性进行修改 例: table.render({ elem: '#messageTable' ,id: 'search_table_mId' ,height: 500 ...

  3. victory-native的使用

    Victory用于构建交互数据可视化的可组合React组件的生态系统 想写又不想写,真尴尬...

  4. Ansible配置免密登陆

    0x01:  把远程服务器的公钥来获取到本地 #ssh-keyscan ip1 ip2 ip3 ip4 >> /root/.ssh/known_hosts 完成后,/root/.ssh/k ...

  5. MySQL 数据库字段类型使用说明

    简介 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型. 数值类型 下列用于描述的代码字母中: M表示最大的显示尺寸.最大的合法的显示尺寸是 255 .(注 ...

  6. [Swift]LeetCode16. 最接近的三数之和 | 3Sum Closest

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  7. [Swift]LeetCode198. 打家劫舍 | House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. [Swift]LeetCode553. 最优除法 | Optimal Division

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...

  9. python之定义参数模块argparse(二)高级使用 --传参为函数的实现

    我们在文章python之定义参数模块argparse的基本使用中介绍了argparse模块的基本使用方法 当前传入的参数只能是int.str.float.comlex类型,不能为函数,这有点不方便,但 ...

  10. java 网络通信传输层协议——UDP和TCP

    本文原文由作者“zskingking”发表于:jianshu.com/p/271b1c57bb0b,本次收录有改动. 1.点评 互联网发展至今已经高度发达,而对于互联网应用(尤其即时通讯网专注的即时通 ...