Get the outside IP of the server the webapp is hosted on - javascript

How can I get the global IP of the server?
I could do a http request to 'http://wtfismyip.com/json' and get the IP address.
Is there a way to get the IP (or dns) whitout doing an outside request?
I can't find anything on the 'process.env' or 'os' object.
I use nodejs with express and deploy to heroku.

Simple answer: No. You'll need some kind of outside source. That source could be as local as the router if you had access to it, but it's more work than it's worth. And if you're on a hosting solution that's out anyway.
I assume your reason for wanting to get this quickly is for speed optimization? If so then I recommend you not worry about it. It's such a small action it won't have any lasting impact.
Now that I think about it; you should already know your public IP! If it's a hosted solution it ought not to change so you can get it once and just hard-code it into your project. I have no experience with heroku though, so it's possible they use a dynamic IP.

Related

Web Application using Ember.js and the dynamic ip backend API

I'm playing more and more with Ember and I really like it.
It's new to me but guides are solid and everything.
I understand how to pin-point the API Server IP so Ember can use it, but I would like to make it more dynamic - The idea is that the API Server is on LAN network (or on dynamic IP WAN - but I don't want ddns for that)
The problem is that I currently have to type IP address then deploy and that version work with that one IP address. I would love to be able to type in IP address on first? and then use that ip as the endpoint for api server.
I'm more than sure this is somehow possible but I lack the examples out there.
I understand that there should be application/index.js route that would check some kind of 'store' to check if there is ip address with array that was saved locally in file?/cookie? and if there is one then somehow load that ip to the adapters/application.js host?
Use an initializer if you have to initialize something.

Deploying AngularJS App: How to make app read JSON data

I'm stumped. So I've written a nice little web app that gets all its data from a JSON that has one array in it. To bridge the AngularJS-parts and the data, I have this line in my services.js
.constant("baseURL","http://localhost:3000/")
This of course only works with me locally. But I haven't considered that I might have to get my web host to serve up the data.
Now, I'm a complete beginner at this. I've managed to code my app with moderate effort, having only started coding in January. So I'm completely out of my league here. I don't even know what I'm looking for. I mean, I have to replace the localhost:3000with something. But if I do http://domain.com/db.json that obviously doesn't work.
I've talked to Godaddy's tech support and they go "Nuh-uh, not helping" the second I show them this line of code. Because, to be honest, I don't even know what the address I'm looking for is called. Is it a URI?
I have a few questions regarding this and I turn to you, because - this story gets worse - there's a possible job on the line for me. Not a coding thing, but the data that is parsed from the JSON into the site.
What is the address I'm looking for called?
How do I get my hosting to serve up JSON data? I mean, I don't even know what I need to ask the tech support people here. Do I need a JSON server?
If they don't offer it, where could I get my JSON data served in a way that I can push more articles into the array and preferrably edit it, too?
Thank you so much!
EDIT #1
If it helps, here's the contents of my /webroot/personalfolder
You will need to change your localhost:8000 to your domain name. For example, if you bought www.foobar.com then your base URL would just become www.foobar.com On a rented web server (like GoDaddy Hosting) ports 80 and 443 are normally open so you wouldn't need to specify this. If you are self hosting, you will need to open these ports on your router and apply port forwarding rules to your PC.
When you host your published website, you will just put your JSON file in with your Website Source. For example your structure may look like this:
AngularAppFolder/index.html
js
csss
Just add another folder called db place your JSON file in there, and reference it in your Angular code, no need to worry, its just another file just like your HTML or CSS which makes up your website. Remember though that your JSON data will be publicly available here!
If your looking for an alternative way of getting your JSON data, you may want to look into creating a REST API. This uses a web server to dish out data depenant on the HTTP request you send to it. but that's a very big topic! Here is some reading into it if your feeling brave! Node.js REST API tutorial
Final note: if you are self-hosting this application (running it on your own computer or server) then you will need to change the DNS record for your domain name to the global IP address of the server! you shouldn't have to worry if your using the hosting offerings from a company such as GoDaddy however.
You need to change the baseURL value to whatever the URL on which the site is hosted. Something like http://<mysite>.com.

Meteor: How to get Ip address for custom tracking system

I am very confused about ip addresses and headers. So I'm sorry if my question seems simple.
I have gone through the answers listed here:
How to get the user IP address in Meteor server?
But I can't figure out how to make them work. For example, I am trying to use this:
Meteor.onConnection(function(conn) {
console.log(conn.clientAddress);
});
But nowhere can I find where to place it.
If I place it on the client side it says Meteor.onConnection is undefined. If I place it on the server nothing happens. I don't think you run it on meteor.startup.
Is there someone could give me a hand in explaining where to use this code? Specifically if anyone has built a custom tracking system, that would be great. Do you store ips into database? or how to record for tracking purposes?
Any hint will help! thanks
Place the code anywhere on the server. e.g. in server/main.js. There is no need to call it from within Meteor.startup.
Start the app.
Open your browser to the appropriate address (e.g. http://localhost:3000) - this will cause a web client to connect and run the code from (1).
Look at your shell output (the shell you used to start the app - not the browser console) and you should see your local IP address (127.0.0.1) printed.
This should work in production as well, however if your app is served behind a proxy (e.g. nginx) you will need to add the appropriate commands to pass the user's IP to your app. For nginx, see this post.

How can I use a JSON API in JavaScript?

I'm trying to write a local application in JavaScript to fetch some JSON data from a site, parse it, and display some statistics for me. So far I have been unable to get it to work, or even able to pull JSON from any site anywhere. Is this because of the same origin policy? I thought this would be relaxed for a locally stored .html file on my own machine.
The site I am trying to use doesn't appear to support JSONP or CORS.
I would have thought this was a common scenario and am surprised that I can't get it to work, although I'm not very experienced with JavaScript. I tried using XMLHttpRequest, and I also taught myself quite a bit of JQuery, but I am unable to get any simple access to work.
Is what I am trying to do impossible?
In JQuery, my basic access looks like this - the alert popup never gets called.
$.getJSON(url,function(result) {
alert("success");
});
I thought this would be relaxed for a locally stored .html file on my own machine.
No, the opposite is usually true – local files usually can’t even request other local files via Ajax.
Anyways, yes, it is probably because of the same origin policy. For a local thing, what you can do is proxy the page you need through some server in some language and also use that server to serve your files. Nginx can, in fact, do that all on its own; the configuration would look something like the below. You could also use curl and CGI.
location = /proxymypage {
proxy_pass http://remote-site.com/data.json;
}
location / {
root /path/to/your/files;
}

Starting a couchdb DB in a HTML + Javascript web

I have a website built with HTML and JavaScript, served by Tomcat.
I need to add a no-SQL database to it, and I'm trying Couch DB. I'm in trouble, 'cause I understand how the DB works, but I have no idea how to connect it with my website.
I'm used to SQL, where I just have to make a connection and then send SQL queries. How can I instantiate a connection object to CouchDB, and use couchdb.js? Currently, Tomcat answers me with a cross domain issue, 'cause Tomcat and couchdb are on different ports.
Can anybody help me with the very basics?
Cross-origin resource sharing (CORS) will be implemented only in the next release of CouchDB (v.1.3).
It could be surprising but one should realize that it is not the "very basics" of CouchDB:
Most of CouchApp web apps developers serve their Ajax applications from CouchDB itself (using HTML shows, lists, and attached static pages).
Some developers use CouchDB as a simple database within a 3-tier architecture.
Others send requests to CouchDB from desktop or signed applications (Java, Flash, etc.).
So, sending Ajax queries from code retrieved from external sites was relatively rare.
My recommended advice would be to adopt one of the most common settings.
If it is definitely not suited to your case, then you can either test CouchDB's development version, or use a proxy so that CouchDB appears to be on the same server as your HTML code (until the next release).
First, you need to get familiar with curl, then you start to test your couchdb through command line.
Trying on the same machine that you installed couchdb.
$ curl localhost:5984
After that, try to access from a different machine using curl:
$ curl http://<your_couchdb_server_name_or_ip>:5984/
If you can't, you need to check if your server has a firewall that are blocking outside requests to your server on 5984 port.
Now, to access your couchdb from a ajax request you must configure the local.ini file:
[httpd]
enable_cors:true;
[cors]
credentials:true;
mehods:PUT,GET,POST,DELETE,OPTIONS;
origins:*;
Restart your couchdb and try again, this must fix you problem.

Categories