节点js中response.send和response.write之间的区别

编程入门 行业动态 更新时间:2024-10-07 18:22:08
本文介绍了节点js中response.send和response.write之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写了一个使用Node jsrestify框架的小API。此API接收请求(实际上是/之后的任何内容),然后将该请求发送到另一台服务器。从服务器获取响应并将响应传递回原始请求源。对于这个API,我使用的是restify服务器和客户端。

I have written a small API which uses the Node js "restify" framework. This API receives a request (actually anything after "/") and then send that request to another server. Get the response back from server and passes the response back to original source of request. For this API I am using both restify server and client.

以下是用于更好理解的API代码。

Below is that API code for better understanding.

var apiServer = require('apiServer'); apiServer.start(); var restify = require('restify'); var assert = require('assert'); function onRequest(request, response, next) { var client = restify.createStringClient({ url: 'example' }); client.get('/' + request.params[0], function(err, req, res, data) { assert.ifError(err); response.setHeader('Content-Type', 'text/html'); response.writeHead(res.statusCode); response.write(data); response.end(); }); next(); } function start() { var server = restify.createServer(); server.get(/^\/(.*)/, onRequest); server.listen(8888); console.log("Server has started."); } exports.start = start;

现在我需要知道 response.write 和 response.send 。因为使用 response.write 我可以设置标头并在其中写入但是当我使用 response.send时,无法对标头执行任何操作。当我使用 response.send 与 setHeader()或 writeHeader()我收到此错误:

Now I need to know the difference between response.write and response.send of Node.js. Because with response.write I can set header and write in it but it is not possible to do anything with headers when I use response.send. When I use response.send with setHeader() or writeHeader() I get this error:

http.js:691 throw new Error('Can\'t set headers after they are sent.'); ^ Error: Can't set headers after they are sent.

还有另外一件事。使用 response.send()我在屏幕上获得完整的HTML输出,如:

There is also another thing. With response.send() I get the complete HTML output on the screen like:

<!DOCTYPE html>\n<html>\n\t<head></head></html> ..... "bla bla bla"

但是 response.write 我没有在屏幕上看到html但只有文字bla bla bla。

But with response.write I do not get the html on screen but only the text "bla bla bla".

如果有人能解释我的差异会很棒。

It would be great if someone can explain me the differences.

推荐答案

我找不到 docs 中的response.send(),但我假设 .send()将填写并发送响应,因此只能调用一次,而 .write()将只是写回复,但你必须使用 response.end()

I can't find response.send() in the docs, but I assume .send() will fill in and send the response so can only be called once, whereas .write() will just write the response, but you have to send it using response.end()

发送它这意味着你可以编辑标题使用 .write(),因为尚未发送回复。

This means you can edit the headers using .write() because the response has not been sent yet.

编辑:

response.send()是 restify Response API wrapper

更多推荐

节点js中response.send和response.write之间的区别

本文发布于:2023-11-25 06:22:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   区别   js   response   send

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!