I'm trying to parse the url in node js. Getting null values from this code. It is receiving value for the path. But not for the host or protocol.,
var http = require('http');
var url = require('url');
http.createServer ( function (req,res){
var pathname = url.parse(req.url).pathname;
var protocol = url.parse(req.url).protocol;
var host = url.parse(req.url).host;
console.log("request for " + pathname + " recived.");
console.log("request for " + protocol + " recived.");
console.log("request for " + host + " recived.");
res.writeHead(200,{'Content-Type' : 'text/plain'});
res.write('Hello Client');
res.end();
}).listen(41742);
console.log('Server Running at port 41742');
console.log('Process IS :',process.pid);
The HTTP protocol doesn't communicate the combined URL in one value for Node to parse out.
A request to http://yourdomain.com/home, for example, arrives as:
GET /home HTTP/1.1
Host yourdomain.com
# ... (additional headers)
So, the pieces won't all be in the same place.
The path and query-string you can get from the req.url, as you were doing – it'll hold "/home" for the above example.
var pathname = url.parse(req.url).pathname;
The host you can get from the req.headers, though a value wasn't always required for it.
var hostname = req.headers.host || 'your-domain.com';
For the protocol, there isn't a standard placement for this value.
You can use the advice given in "How to know if a request is http or https in node.js" to determine between http and https.
var protocol = req.connection.encrypted ? 'https' : 'http';
Or, though it isn't standard, many clients/browsers will provide it with the X-Forwarded-Proto header.
req.url only contains the path not the entire url.
Rest are in request headers.
For Host: console.log(req.headers["host"]);
For Protocol: console.log(req.headers["x-forwarded-proto"]);
you can view this source code:
https://github.com/expressjs/express/blob/master/lib/request.js
Related
I have deployed (hosted) cors-anywhere in Heroku, but i don't know how to customize it.
For example, I want to add a site link in whitelist.
I get data from this link: http://fmon.asti.dost.gov.ph/dataloc.php?param=rv&dfrm=null&dto=null&numloc=1&data24=1&locs[]=711
How will I do it? I have tried touching the server.js file:
// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
//var port = process.env.PORT || 443;
var port = process.env.PORT || 8080;
// Grab the blacklist from the command-line so that we can update the blacklist without deploying
// again. CORS Anywhere is open by design, and this blacklist is not used, except for countering
// immediate abuse (e.g. denial of service). If you want to block all origins except for some,
// use originWhitelist instead.
var originBlacklist = parseEnvList(process.env.CORSANYWHERE_BLACKLIST);
//var originWhitelist = ['http://fmon.asti.dost.gov.ph/dataloc.php','https://fmon.asti.dost.gov.ph/dataloc.php','http://fmon.asti.dost.gov.ph','https://fmon.asti.dost.gov.ph'];
var originWhitelist = parseEnvList(process.env.CORSANYWHERE_WHITELIST);
function parseEnvList(env) {
if (!env) {
return [];
}
return env.split(',');
}
// Set up rate-limiting to avoid abuse of the public CORS Anywhere server.
var checkRateLimit = require('./lib/rate-limit')(process.env.CORSANYWHERE_RATELIMIT);
var cors_proxy = require('./lib/cors-anywhere');
cors_proxy.createServer({
originBlacklist: originBlacklist,
originWhitelist: originWhitelist,
requireHeader: ['origin', 'x-requested-with'],
checkRateLimit: checkRateLimit,
removeHeaders: [
'cookie',
'cookie2',
// Strip Heroku-specific headers
'x-heroku-queue-wait-time',
'x-heroku-queue-depth',
'x-heroku-dynos-in-use',
'x-request-start',
],
redirectSameOrigin: true,
httpProxyOptions: {
// Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
xfwd: false,
},
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
but when I access the data and look at the console log, it returns a 403 error which is forbidden.
NOTE: When you say self hosted CORS it will only work for your site to
proxy. CORS setting on your server is for you not for the list of
sites you mentioned. They will be having their own CORS filters setup.
403 actually refers to the forbidden resource rather than a CORS Issue. Cors issue will look something like as follows:-
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'yourUrl' is therefore not allowed access. The response had HTTP status code 400
For cors-anywhere the whitelisting code is pretty simple as mentioned below:-
// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 8080;
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: ['http://fmon.asti.dost.gov.ph'], // Allow all origins
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2']
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
This should Ideally work for you with your application calling this somewhere.
If you are getting 403 while accessing this URL from your application
then be sure the URL you mentioned is protected and you must get
proper authentication done before requesting it.
I spent 3 days looking for the cause, got a 403 error for some sites and realized that the problem may be that they can not accept requests from "origin" different from theirs.
I just tried removing those headers on the proxy server and everything worked!
removeHeaders: ['origin', 'referer']
I am adding HTTPS to a node.js application, while forwarding HTTP to HTTPS. I am using the following bit of code:
// Trusting proxy
self.app.enable('trust proxy');
// Http -> Https redirection middleware
self.app.use(function (req, res, next) {
var schema = req.protocol.toLowerCase();
console.log("Redirection check, schema: " + schema);
if (schema === 'https') {
console.log("Next");
next();
} else {
var tmp = 'https://' + req.headers.host + req.url;
console.log("Redirect to: " + tmp);
res.redirect(tmp);
}
});
All works fine when I browse https://localhost:8443/index.html, the page opens fine.
But when I try http://localhost:8443/index.html, Chrome seems to redirect or rewrite the url to localhost:8443/index.html and Chrome waits for localhost eternally. There is no console message from my application. I am on under windows 7 with Chrome.
What is wrong with my code?
Chrome is just hiding the http:// portion of the URL, this is normal.
You can't run http and https on the same port.
You've specified http in the address, so the browser tries to connect with HTTP. The server is listening for HTTPS so it fails.
Whenever I make http requests to a server using node.js, the console.logs inside the http request function output twice. I set up a sever with the following code, and used localhost:8888 on firefox to make a request (localhost:8888):
var http = require('http');
var url = require('url');
function onRequest(request, response) {
var pathname = url.parse(request.url, true).pathname;
console.log("Your url pathname is " + pathname);
response.write("Did you get your response?");
}
var new_server = http.createServer(onRequest).listen(8888);
The console prints:
Your url pathname is /
Your url pathname is /favicon.ico
My questions are:
Why is the request sent twice?
Why is the pathname favicon.ico on the second request despite the fact that I did not specify anything after the socket number in the request url?
Is there any way you can fix these two issues?
Thank you.
Obviously the request isn't sent twice, it's two requests.
It's the browser asking for the favicon. That's a thing browsers do.
Hi I´m trying to figure out how to login to a website with node js.
Even if I get back a 200 OK , I'm NOT logged in to the page.
The loggin is normally via form on the page, but for complex functionality it should work via nodes.
Here is my actual code, is there something wrong with the code ?
var express = require('express'),
request = require("request"),
https = require('https'),
http = require('http');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'jade');
app.use(app.router);
});
var auth = 'Basic ' + new Buffer("usernameString:passwordString");
var options = require('url').parse( "https://example.com/path" );
options.rejectUnauthorized =false;
options.host="example.com";
options.path="https://example.com/usr/login";
options.defaultEncoding = 'utf8';
options.headers= { 'content-type' : 'application/x-www-form-urlencoded',
"Proxy-Authorization": auth,
host: "example.com",'accept-encoding': '*'
};
options.agent = new https.Agent( options );
var req = https.get( options, function(res) {
console.log(res.statusCode); //200 but not logged in ?!?
});
UPDATE:
If I test the login with an AJAX request, everything works fine but I need NODE JS:
Here the JavaScript code with ajax:
$.ajax{(
type: get,
…
…
data: "LoginForm[username]=usernameString&LoginForm[password]=passwordString"
…)};
Any suggestions ?
In most cases authenticating service will return a cookie which you have to include in every following requests so that the remote service recognize you ...
As far as I know, proxy authentication works the same as basic authentication, in that a client is never really logged in (by means of a session being held by the server), but just sends the authentication headers along with every request.
So try sending the Proxy-Authorization header along for every request you make.
EDIT: okay so perhaps you don't need to use proxy authentication at all. I also noticed that your options.path is incorrect: it should only contain the path name, not a full URL.
Try this:
options.path = "/usr/login?LoginForm[username]=usernameString&LoginForm[password]=passwordString";
I'm new to node.js and I'm trying to write a program that receives http requests and forwards the contents through a socket. At the other end of the socket is a paging system that needs messages terminated with a new line character. So far, it works fine, except there is an extra message sent with the contents: undefined.
When I print the contents of the pager message to the client's browser, there does not appear to be a new line. Am I doing this right?
sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
net = require("net");
socket = new net.Socket();
socket.connect(4000, '192.168.0.105');
var httpServer = http.createServer(function(request, response) {
var uri = String(url.parse(request.url).query);
var message = uri.split("=");
var page = 'FPG,101,0,3!A' + message[0] + '\n';
response.writeHead(200, {"Content-Type":"text/html"});
response.write('sending message: ' + page + " to pager");
response.end();
socket.write(page);
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
EDIT: I narrowed it down further. It turns out that if I do:
var page = 'FPG,101,0,3!A' + 'hello' + '\n';
It works okay. So the output of uri.split("=") must not be what I am expecting.
I'm sure the new line is there, but you aren't going to see it when you send your Content-Type as text/html. In HTML, \n is just other whitespace, and is treated as such. Use text/plain instead.
Since content type is 'text/html',we can happily use break statement.Just like this
res.write('hello'+'<br/>');
res.write('nice to meet you');
You can try this code in node:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('hello,this is saikiran'+'<br/>');
res.end('nice to meet you');
}).listen(process.env.PORT || 8080);
console.log('server running on port 8080');
As per content type of text/plain, you can also use \r to send a return input
to the client's browser
I figured out what the problem was. For some reason the browser was sending an additional request to the page right after my request. I just filtered for requests with a certain format and that fixed it.
Consider using \\n instead of \n for new line.