nodejs's http.get by providing URI - node.js

I've been reading the http API but I couldn't find a way to do an http.get() by just providing the URI I want to access.
For example, I receive from client a complicated URI, say http://a.b.c:1234/d/e and I would like to do a GET request to it. The problem is that if I have to set the options parameter of the http.get() method I'm going to end up in complicated string parsing to parse host, port and path. Moreover, I'm not even sure port will always be provided, for example. Is there a way to do an http.get() by just providing the full URI?
Thanks!

You can use the url library to do this.
var parse = require('url').parse;
http.get(parse(myurl));
will work, because the properties on the parsed url object are the same as the options expected by http.get. This isn't a hack, if you look in the docs it says that the two libraries are designed to complement one another like this.

Use url.parse:
var url = require('url');
var http = require('http');
function download(uri, cb) {
http.get(url.parse(uri), cb);
}

Related

Regex to extract URL domain name with a dash in it using Node.js

I am trying to extract just the websites name from a URL and all the stackoverflow answers haven't led me anywhere.
My URL is in the following format:
https://order-dev.companyname.com
I just want to extract order-dev from the URL.
Got this to work for me (?<=\/\/)(.*?)(?=\.)
If you have an express framework with your node app then it is easy to get a domain/list of subdomains directly from the incoming request..
Using Express Framework
req.hostname() // returns 'example.com'
req.subdomains() // return ['ferrets', 'tobi'] from Host: "tobi.ferrets.example.com"
For plain NodeJS
All Incoming request will contain the following header origin which can be extracted using the following request.headers.origin in any functions where you handle your incoming request MDN LINK
In most JavaScript engines (i.e., most browsers (excluding IE) and Node) you can also use URL for this, specifically URL.host combined with String.prototype.split():
const siteUrl = 'https://order-dev.companyname.com';
const myURLObject = new URL(siteUrl);
const subdomain = myURLObject.host.split(".")[0];
console.log(subdomain); // "order-dev"

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response.
I'm using KoaJS. From what i understand, Koajs merges the request and response object into ctx but when reading through the docs I can't find anything along the lines of ctx.send or similar to send a http response.
Can anyone give me some direction or links.
Thanks.
To send the body of a response, you can simply do ctx.response.body = 'Hello'. There are many aliases attached to ctx, so you don't necessarily have to reference the response or request yourself. Doing ctx.body = 'Hello' would be the same as the code above.
If you wanted to set headers, you would use the ctx.set() method. For example: ctx.set('Content-Type', 'text/plain').
To access the query parameters, you would use ctx.request.query['some-key'] (or simply the alias ctx.query['some-key']).
All of the different request/response methods are documented pretty well at the Koa website along with a list of aliases attached to ctx. I highly recommend you give it a read.

Possible to include body in get request? - Node Request library

Is it possible to use the request library in node to include a body for a get request? https://github.com/request/request#requestoptions-callback
It looks like the body option only works for POST/PUT/PATCH methods according to documentation. I was wondering if there was a known workaround for this. I know this is not conventional but the api that I will be hitting does accept a get request with a body and putting the data in query string is not an option because the url becomes too long. (I do not have the ability to implement api changes)
Turns out Node's request library does accept body in the get request although it doesn't mention it in the documentation. Just passing in options.body = {}, with options.json = true, worked great.

Access custom request headers node express

I am building a web api with Express and have not found information on accessing incoming custom request headers.
I will be expecting, for instance, that an incoming post request have a provider_identifier header. When I receive the request, I need to access that header information to validate their subscription.
Can someone point me in the right direction/provide advice on this?
router.post('myendpoint/', function(req, res){
var providerId = req.????;
});
Answering my own question here... was kindof a DUH moment for me.
Using above example, simply reference the headers collection like so:
var providerId = req.headers.provider_identifier;
One note: Use an underscore rather than a dash. "provider-identifier" doesn't work, but "provider_identifier" does.

Does the request object in Node.js contain any navigator.userAgent type of information?

I've setup a Node.js server that gets some contact form data and forwards it to my email. I'd like to be able to forward some information about the user's browser along with the form data.
Is any of that information contained in the request object in some form ? Kind of like the navigator.userAgent string that is available on the client ?
Or should I include that string in the data sent out, manually, myself?
I was thinking of something like :
var httpServer = http.createServer(function (request, response)
{
var browserID = request.navigator.userAgent;
});
Thanks!
I was testing this out myself in express, and you can find the user-agent string in:
request.header['user-agent']
and you can see it in the HTTP specification in 14.43 here.
In the future, you can simply examine the request object either with console.log() or with the debugger and see exactly what's in it. I find that is often ultimately more educational than trying to find things in documentation somewhere.

Resources