Handler response in nodejs - node.js

I am new in NodeJS, and I am working an an example:
function test(req,res){
var path = urls[Math.floor(Math.random()*urls.length)];
console.log("try to redirect to:"+path);
http.get(path,function(res_server){
//how to send the data from res_server to res
});
}
And the urls is an array of url.
I wonder how can I send the data from the res_server to the original client response?
BTW, the url maybe http or https.
update
var urls=["url1","url2","url3"];
var path = urls[Math.floor(Math.random()*urls.length)]; // find an random item from the array
update:2
Fine, this is the complete simple test script:
var http=require("http");
http.createServer(function(req, res1) {
var url = 'http://www.google.com.hk/images/srpr/logo11w.png';
var hp=require("http");
hp.get(url, function(res2) {
res2.pipe(res1);
});
}).listen(3000);
It works, but if you change http://www.google.com.hk/...logo..png to https:/www.google.....png
It will throw error:
http.js:1840
throw new Error('Protocol:' + options.protocol + ' not supported.');
^
Error: Protocol:https: not supported.
at Object.exports.request (http.js:1840:11)
at Object.exports.get (http.js:1847:21)
at Server.<anonymous> (C:\Users\maven\Desktop\t.js:6:6)
at Server.EventEmitter.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:525:27)

Change var http = require('http'); to var http = require('https');

I do not fully understand your example. Looks strange to me. However best would be to pipe the request response into the server response:
http.createServer(function(req, res1) {
var path = url.format({
protocol: 'http:',
host: 'www.google.com'
});
http.get(path, function(res2) {
res2.pipe(res1);
});
}).listen(3000);

Related

Nodejs Event error Express

i have this code in express.
var express = require('express');
var http = require("http");
var https = require("https");
var app = express();
var optionsSB = {
host: 'domain.com',
path: '/wp-content/themes/domain/includes/ajax/get_properties.php'
};
var optionsLV = {
host: 'apiproperties.local',
path: '/properties/storeSB',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
https.get(optionsSB, function (https_res) {
var dataSB = "";
https_res.on("data", function (chunkSB) {
dataSB += chunkSB;
});
https_res.on("end", function () {
http.request(optionsLV, function(http_res){
var dataVL = "";
http_res.on("data", function (chunkVL) {
dataVL += chunkVL;
});
http_res.on("end", function () {
console.log(dataVL);
});
});
});
});
app.listen(3000, function () {});
I get this error
events.js:183
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:80
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
I already try some things but i dont know what is the problem, regards.
I follow some instruction from a tutorials and all works fine but that error i dont understand.
It will throw like this when you are getting an error in setting up the request with your https.get(), but you don't have any error handler to capture the error. You can supply an error handler:
https.get(...).on('error', function(err) {
// error here
console.log(err);
});
It appears that the specific error is ECONNREFUSED. It could be that the destination is not accepting your https connection or it could be that it doesn't like the way you were passing the options. Since all you have is a host and path, you can also just use the URL:
https.get("https://somedomain.com//wp-content/themes/domain/includes/ajax/get_properties.php", ...);

Error: getaddrinfo ENOTFOUND while making get request to localhost, Nodejs

I am trying to make a het request to etcd instance running in my local trough the node http module.
the code look like this
'use strict';
const express = require('express');
const app = express();
var http = require('http');
const port = 10111;
var encoded_url = encodeURI('/v2/keys/message -X GET');
var options = {
host: 'http://127.0.0.1:2379',
path: encoded_url
};
var callback = function (response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
http.request(options, callback).end();
app.listen(port, () => {
console.log("server started on port " + port);
});
but I am getting the following error
Error: getaddrinfo ENOTFOUND http://127.0.0.1:2379 http://127.0.0.1:2379:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
If I make the same curl request from the terminal I get the result
curl http://127.0.0.1:2379/v2/keys/message -X GET
not able to figure out what is the issue.
By default http.request() use port 80.
Use this instead:
var options = {
protocol: 'http:',
host: '127.0.0.1',
port: 2379,
path: encoded_url
};

Node.js Proxy with custom Http(s) Agent and Connect.js Middleware

I've put together a proxy server in Node that needs the ability to tunnel https requests over tls and that all works. Using the the following two packages this was extremely easy to setup: proxy, https-proxy-agent. My issue is that I'm trying to capture HAR files using connect as a middleware layer and I'm getting the following error:
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.writeHead (_http_server.js:180:21)
at ClientRequest.<anonymous> (/.../node_modules/proxy/proxy.js:233:11)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:362:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
The is as simple as the following and it only seems to happen when I'm using connect and proxying through my local browser(this proxy is actually being used with BrowserStackLocal). When I pass through the proxy from anything other than my local machines browser, it's like it doesn't even know the middleware exists.
So basically, I just need to get connect working in this scenario and I'm not sure if I need to pause something and resume, or what... any ideas would be greatly appreciated. The base code is below:
const path = require('path');
const http = require('http');
const proxy = require('proxy');
const Agent = require('https-proxy-agent');
const connect = require('connect');
const har = require('./har');
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
const server = proxy(http.createServer(middleware));
server.agent = new Agent('http://localhost:8081');
server.listen(8081)
Thanks!
EDIT: Just a note: the har middleware is not modifying headers at all.
proxy hasn't been maintained in a while. Builds are not passing, last commit don't pass tests. The source of the stack trace you've put up is coming from here in Line 233 - buggy library code.
Writing a similar proxy is trivial. Following code illustrates how to create one.
const http = require('http');
const urlP = require('url');
const proxiedServer = 'http://localhost:8888';
// Proxy server
http.createServer((req, res) => {
console.log(`Proxy: Got ${req.url}`);
const _r = http.request(
Object.assign(
{},
urlP.parse(proxiedServer),
{
method: req.method,
path: req.url
}
),
_res => {
res.writeHead(_res.statusCode, _res.headers);
_res.pipe(res);
}
);
req.pipe(_r);
}).listen(3124, () => {
console.log("Listening on 3124");
});
// Regular server. Could be Express
http.createServer((req, res) => {
console.log('Proxied server: ', req.url);
let b = '';
req.on('data', c => {
b += c;
});
req.on('end', () => {
console.log('Proxied server: ', b);
});
res.writeHead(200);
res.end('ok');
}).listen(8888, () => {
console.log('proxied server listening on 8888');
});
Your code using your own custom proxy would look like the following:
const urlP = require('url');
const path = require('path');
const http = require('http');
const connect = require('connect');
const har = require('./har');
const proxiedServer = 'http://localhost:8888';
// Proxy server
http.createServer((req, res) => {
console.log(`Proxy: Got ${req.url}`);
const _r = http.request(
Object.assign(
{},
urlP.parse(proxiedServer),
{
method: req.method,
path: req.url
}
),
_res => {
res.writeHead(_res.statusCode, _res.headers);
_res.pipe(res);
}
);
req.pipe(_r);
}).listen(3124, () => {
console.log("Listening on 3124");
});
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
middleware.use((req, res) => {
console.log('Proxied server: ', req.url);
let b = '';
req.on('data', c => {
b += c;
});
req.on('end', () => {
console.log('Proxied server: ', b);
});
res.writeHead(200);
res.end('ok');
});
http.createServer(middleware).listen(8888, () => {
console.log('proxied server listening on 8888');
});

_http_server.js:192 throw new RangeError(`Invalid status code: ${statusCode}`);

This is my code:
var express = require('express');
var http = require('http');
var redis = require('redis');
var url = require('url');
var client = redis.createClient().setMaxListeners(0);
var app = express();
app.set('port', 3000);
app.get('/*', function(req, res) {
var key = url.parse(req.url).pathname;
client.on('connect', function() {
console.log('connected to redis!');
});
client.get(key, function(err, reply) {
if( reply == null) {
client.set(key, 1);
client.expire(key, 300);
res.send('1');
}
else {
client.incr(key, function(err, reply) {
console.log('increment value: ' + reply);
res.sendStatus(reply);
});
}
});
});
http.createServer(app).listen(app.get('port'), function() {
console.log('listening');
});
This is my output when I run the file ($ node test.js):
I tried this on my ubuntu machine and it perfectly works. This is what I get on my mac. Could someone explain me why this is happening. Any help would be appreciated.
listening
increment value: 2
_http_server.js:192
throw new RangeError(`Invalid status code: ${statusCode}`);
^
RangeError: Invalid status code: 2
at ServerResponse.writeHead (_http_server.js:192:11)
at ServerResponse._implicitHeader (_http_server.js:157:8)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:559:10)
at ServerResponse.send (/Users/sharath/webapps/docker/node_modules/express/lib/response.js:209:10)
at ServerResponse.sendStatus (/Users/sharath/webapps/docker/node_modules/express/lib/response.js:346:15)
at Command.callback (/Users/sharath/webapps/docker/test.js:24:13)
at normal_reply (/Users/sharath/webapps/docker/node_modules/redis/index.js:714:21)
at RedisClient.return_reply (/Users/sharath/webapps/docker/node_modules/redis/index.js:816:9)
at JavascriptRedisParser.returnReply (/Users/sharath/webapps/docker/node_modules/redis/index.js:188:18)
at JavascriptRedisParser.execute (/Users/sharath/webapps/docker/node_modules/redis-parser/lib/parser.js:415:12)
Http response statuses should be integers. It cannot be strings, objects, array or like that and should begin from 100.
From your code i see that you try to do
res.sendStatus(reply);
Check reply variable. From redis incr response im thinking it's string "OK".
Which is bad.. So to fix it just use
res.sendStatus(reply ? 200 : 500);
Also check this.
http://expressjs.com/en/4x/api.html#res.sendStatus
And this
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
EDIT
If you need to send some JSON or data into front-end just do like this
res.json({thisIsMyNumber: reply});
or
res.send({thisIsMyNumber: reply});
Hope this helps.

Node http.request not working

I am trying to create a simple server and a simple client with node.js using http module. Server is working fine but client is not. Please help me to find the bug...
Server Is :
var server = require('http').createServer();
server.on('request', function(req, res){
res.end("hello, world");
});
server.listen(4000);
Client Is :
var options = {
host : 'localhost',
port : 4000,
method : 'GET',
path " '/'
};
require('http').request(options, function(res){
console.log(require('util').inspect(res));
res.on('data', function(data){
console.log(data);
});
I am running them in different terminal windows as node server.js & node client.js.
I am getting below mentioned error on the client.js running terminal after around 10 mins.
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (http.js:1473:15)
at Socket.socketOnEnd [as onend] (http.js:1569:23)
at Socket.g (events.js:175:14)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Thanks !
The request() method of the HTTP library will not automatically end requests, therefore they stay open, and time out. Instead, you should either end the request using req.end(), or use the get() method, which will do so automatically.
var http = require('http');
var req = http.request(options, function(res) {
// handle the resposne
});
req.end();
Or:
http.get(options, function(res) {
// handle the resposne
});

Resources