Node.js http requests and cookies with http module - node.js

How do I use http module to make GET requests with a query string and cookies?
GET someurl?test=one
Cookies: name=john; name1=mary;

var http = require('http');
var url_parser = require("url");
var url = "someurl?test=one";
var url_parts = url_parser.parse(url);
var options = {host: url_parts.hostname, port: url_parts.port|80, path: url_parts.path};
var request = http.request(options, function(response) {
//do something with the response
}).on('error',function(e){
//error happened
});
request.setHeader( 'cookie', YOUR_COOKIE );
request.end();

Related

How do I do a 302 redirection with node-http-proxy?

My code:
var path = require('path');
var util = require('util');
var http = require('http');
var fs = require('fs');
var httpProxy = require('http-proxy');
httpProxy.createProxyServer({
target: 'http://localhost:9000',
agent: https.globalAgent,
headers: {
host: 'localhost'
}
})
.listen(8000);
util.puts('proxy server listening on port 8000');
http.createServer(function targetServer(req, res) {
res.writeHead(302, { 'Location': '/' }); //Here is the 302
util.puts('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
})
.listen(9000);
util.puts('target server listening on port 9000');
when I make the request from my browser i get the following error:
Error: socket hang up
I have no idea what this error means. Can anyone help me figure this out?
I did get it to work.
I had to do 2 things:
Include follow-redirects package (npm install follow-redirects)
And change my http initialization:
To:
var http = require('follow-redirects').http;
from:
var http = require('http');
include the {changeOrigin: true} property in the CreateProxyServer:
var proxy = httpProxy.createProxyServer({changeOrigin: true, toProxy : true, });

expressjs 4.x and https, generate key and certificate

I want to create an https server with express 4.x. Even if a lot of code found on google is based on express 3.x I think I made the port correctly.
Even if I tried to goole it is not very clear to me how to generate the keys. Without the key I'm expecting 401.
I tried with the script found in this gist. But I'm keeping on receiving the error Error: DEPTH_ZERO_SELF_SIGNED_CERT.
I'd like to test it both with curl, request, and super test.
This is what actually I have:
server.js
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./server/server-private-key.pem').toString();
var certificate = fs.readFileSync('./server/server-certificate.pem').toString();
var options = {
key : privateKey
, cert : certificate
}
var app = express();
app.get('/', function(req, res) {
req.client.authorized ?
res.json({"status":"approved"}) :
res.json({"status":"denied"}, 401);
});
server = https.createServer(options,app);
var port = 12345;
server.listen(port, function(){
console.log("Express server listening on port " + port);
});
client.js
var https = require('https');
var fs = require('fs');
var options = {
host: 'localhost',
port: 12345,
method: 'GET',
path: '/',
key: fs.readFileSync('./client/client-private-key.pem'),
cert: fs.readFileSync('./client/client-certificate.pem'),
headers: {}
};
var req = https.request(options, function(res) {
console.log('dudee');
console.log(res);
});
req.end();
With cURL you can use the -k flag to bypass the self-signed cert problem.
With request you can just set rejectUnauthorized: false in the request options.

How to send parameters to Node.JS basic server

I am learning Node.JS and this is the most commonly available example of server by Node.JS
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
// var name=request.getParameter('name');
// console.log(name);
console.log('res: ' + JSON.stringify(response.body));
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
Now when I am executing this from console it works fine, and from browser also it works fine, by hitting the URL: localhost:8000
But now I also want to send some parameters to this server, so I tried localhost:8000/?name=John and few more URL's but none of them work, Can anyone help me?
Thanks in advance!!
try:
var url = require('url');
var name = url.parse(request.url, true).query['name'];
Node's HTTP API is rather low-level compared to other frameworks/environments that you might be familiar with, so pleasantries like a getParameter() method don't exist out of the box.
You can get the query-string from the request's url, which you can then parse:
var http = require('http');
var url = require('url');
var server = http.createServer(function (request, response) {
var parsedUrl = url.parse(request.url, true);
var query = parsedUrl.query;
console.log(query.name);
// ...
});

Simple node.js proxy by piping http server to http request

Trying to learn more about node.js by making a simple http proxy server. The use scenario is simple: user -> proxy -> server -> proxy -> user
The following code works until the last step. Couldn't find way to pipe connector's output back to the user.
#!/usr/bin/env node
var
url = require('url'),
http = require('http'),
acceptor = http.createServer().listen(3128);
acceptor.on('request', function(request, response) {
console.log('request ' + request.url);
request.pause();
var options = url.parse(request.url);
options.headers = request.headers;
options.method = request.method;
options.agent = false;
var connector = http.request(options);
request.pipe(connector);
request.resume();
// connector.pipe(response); // doesn't work
// connector.pipe(request); // doesn't work either
});
Using tcpflow I see the incoming request from the browser, then the outgoing proxy request, then the server response back to the proxy. Somehow i couldn't manage to retransmit the response back to the browser.
What is the proper way to implement this logic with pipes?
you dont have to 'pause', just 'pipe' is ok
var connector = http.request(options, function(res) {
res.pipe(response, {end:true});//tell 'response' end=true
});
request.pipe(connector, {end:true});
http request will not finish until you tell it is 'end';
OK. Got it.
UPDATE: NB! As reported in the comments, this example doesn't work anymore. Most probably due to the Streams2 API change (node 0.9+)
Piping back to the client has to happen inside connector's callback as follows:
#!/usr/bin/env node
var
url = require('url'),
http = require('http'),
acceptor = http.createServer().listen(3128);
acceptor.on('request', function(request, response) {
console.log('request ' + request.url);
request.pause();
var options = url.parse(request.url);
options.headers = request.headers;
options.method = request.method;
options.agent = false;
var connector = http.request(options, function(serverResponse) {
serverResponse.pause();
response.writeHeader(serverResponse.statusCode, serverResponse.headers);
serverResponse.pipe(response);
serverResponse.resume();
});
request.pipe(connector);
request.resume();
});
I used the examples from this post to proxy http/s requests. Faced with the problem that cookies were lost somewhere.
So to fix that you need to handle headers from the proxy response.
Below the working example:
const http = require('http');
const acceptor = http.createServer().listen(3128);
acceptor.on('request', function(request, response) {
const req = service.request(options, function(res) {
response.writeHead(res.statusCode, res.headers);
return res.pipe(response, {end: true});
});
request.pipe(req, {end: true});
});

Node.js caching proxy server

I am trying to create a http caching proxy server using node.js , where i could forward to any webpages and cached them on my local disk !
The following is my first attempt code :
var http = require('http'),
url = require('url'),
sys = require('url');
var fs = require('fs');
var port = "9010";
// function notFound
function notFound(response){
response.writeHead(404, "text/plain");
response.end("404 : File not Found");
}
//create simple http server with browser requet and browser response
http.createServer(function(b_request, b_response){
//Parse the browser request'url
var b_url = url.parse(b_request.url, true);
if(!b_url.query || !b_url.query.url) return notFound(b_response);
//Read and parse url parameter (/?url=p_url)
var p_url = url.parse(b_url.query.url);
//Initialize Http client
var p_client = http.createClient(p_url.port || 80, p_url.hostname);
//Send request
var p_request = p_client.request('GET', p_url.pathname || "/", {
host: p_url.hostname
});
p_request.end();
//Listen for response
p_request.addListener('response', function(p_response){
//Pass through headers
b_response.writeHead(p_response.statusCode, p_response.headers);
//Pass through data
p_response.addListener('data', function(chunk){
b_response.write(chunk);
});
//End request
p_response.addListener('end', function(){
b_response.end();
});
});
}).listen(port);
console.log("Server running at http://127.0.0.1:" +port + "/");
i want to use any cached library for my app suchas : Node-static(https://github.com/cloudhead/node-static), Static cache, ....
if website that i visited is working fine , my app will forward to it . If not my app will get and return me data that cached on my disk .
is there any solutions for this works ?
thank !

Resources