I wrote a small API that uses the Node js "restify" framework. This API receives the request (in fact, something after the "/"), and then sends the request to another server. Get the response from the server and send the response back to the original request source. For this API, I use both server and client recovery.
Below is the API code for a 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: 'http://example.com' }); 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;
Now I need to know the difference between response.write and response.send from Node.js. Because with response.write I can set the title and write in it, but when using response.send I can do nothing with the headers. 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.
There is another. With response.send() I get the full HTML output on the screen, for example:
<!DOCTYPE html>\n<html>\n\t<head></head></html> ..... "bla bla bla"
But with response.write I do not get html on the screen, but only the text "bla bla bla" .
It would be great if someone could explain the differences to me.
user3275959 Feb 13 '14 at 9:11 2014-02-13 09:11
source share