response.writeHead
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的更多相关文章
- 记录nodejs的writeHead
使用response.writeHead()时,如果第二个参数的值使用错误的字符时,会使整个页面被镶嵌在<pre></pre>中被传输过去. 例: response.write ...
- response.write
response.write(chunk[, encoding][, callback])# 查看英文版 chunk <string> | <Buffer> encoding ...
- 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" ...
- 如何使用IntelliJ集成nodejs进行接口测试<response demo>
1.相关的配置及安装,在如下地址进行参考,这位大师写得也挺好(mac和windows都可以下载) https://www.jianshu.com/p/dd2d2d0ff133 2.在集成nodejs的 ...
- fs.createReadStream(filepath).pipe(response);这句是什么意思?
'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...
- node——request和response的常用对象
request(http.IncomingMessage)和response(http.ServerResponse)对象介绍 request:服务器解析用户提交的http请求报文,将结果解析到req ...
- node.js入门学习(二)MIME模块,request和response对象,demo之不同url请求不同html页面,页面包含图片、样式css等静态资源
一.构建http服务程序-根据不同请求做出不同响应 // 加载http模块 var http = require("http"); // 创建一个http服务对象 http.cre ...
- 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
问题描述 在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store) 效果类似于本地 ...
- nodejs进阶(4)—读取图片到页面
我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...
随机推荐
- linux 使用sh@d0ws0cks server
[root@linux-node1 ~]# cat /etc/shadowsocks.json { "server":"x.x.x.x", , "lo ...
- SSIS - 9.文件系统任务
文件系统任务是用来操作服务器上的文件和目录的.比如,可以新建任务来创建.复制.删除或移动一个文件或一个目录. 一.操作和属性 一个文件系统可以定义如下10种操作. 所有的操作包含Name, Descr ...
- linux 完全关闭tomcat
由于直接调用tomcat的 shutdown.sh 有时无法完全关闭掉tomcat,使用 ps -ef | grep tomcat 查找发现tomcat依然还存在,并未完全关掉.在 catalina. ...
- JAVA基础—线程池
推荐文章java多线程基础 线程池概述 为什么要使用线程池 1.服务器创建和销毁工作线程的开销很大 2.如果频繁的创建和销毁线程会导致频繁的切换线程,因为一个线程被销毁后,必然要把CPU转让给另一个已 ...
- [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- grep的正则表达式结合的几个典型应用
一 几个特殊的字符: ^ :只匹配行首 如^a 匹配以a开头的行abc,a2e,a12,aaa,...... example: grep "^a" //列出所有以a开头的行 $ ...
- BBS论坛(三十)
30.显示评论和添加评论功能完成 (1)apps/models.py class CommentModel(db.Model): __tablename__='comment' id=db.Colum ...
- ionic4+angular6 混合移动开发 capacitor cordova
首先要更新或者安装 ionic cli npm install -g ionic 创建项目 ionic start ionic-angular tabs --type=angular –type=an ...
- 【netty】(2)---搭建一个简单服务器
netty(2)---搭建一个简单服务器 说明:本篇博客是基于学习慕课网有关视频教学.效果:当用户访问:localhost:8088 后 服务器返回 "hello netty"; ...
- C# for Python(Nugut Iron包)
cInronPython是一种在.NET和Mono上实现的Python语言,使用InronPython就可以在.NET环境中调用Python代码 安装InronPython Python: port ...