I have developed server application in node.js. Right now I access the application using 128.1.1.5:3000. But i want to use a domain name like abc.net to access the application. How can I do this. Please suggest.
To configure DNS on your local app,you need to do following configuration.
Make an entry of this DNS example abc.net as a host instead of local host while setting up your node server where you are mentioning the localhost host and port detail eg. in app.js file.
Example
const http = require('http');
const hostname = 'abc.net';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Now open command prompt and type
ipconfig -all
It will list all your IPs. Select the ip of your machine which is preferred one.mostly you can locate this ip by finding the ip which is followed by (preferred) keyword in command prompt.
Now copy this IP address and make an entry of this in system host file.Make sure you have an admin rights to make changes in this file.
Path of host file in Windows
C:\Windows\System32\drivers\etc\hosts
Edit this file and scroll to the end and press Enter to copy the ip address corresponding to the DNS which you have configured in node js application as shown below in new line.
IPaddress(fetched in step 3)
abc.net
i.e ipaddress then give space then dns name
Save the file.
Start your node application.
Now try hitting your api from the url abc.net:port/api
You will need a domain that you can edit the DNS settings on and add an A record that is configured to your server's external IP address
then you can access your domain with the port attached
example: mydomain.com:5000
you should refer to your domain record provider's documentation on how to do this.
Beyond that, you may encounter firewall settings, port settings, and possible HTTPS certificates issues but these are separate topics each.
Related
I want to create a private backend for an application I want to make, but I am having trouble connecting to my node server, I have the basic stuff right now,
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 300.')
But this only works for https://localhost:3000/, how do I make it so that if I have a separate computer on a separate connection, I can connect to this server?
I am not interested in just opening it to everyone but just to specific client IP's...
If the client IP's are on the same network as you, you can check out this question
If you want people from anywhere to access your application, I suggest hosting it on something like Heroku (very easy to deploy, pretty good free tier). You can then create a whitelist of IPs in your express application.
I would suggest for any port forwarding using ngrok or configuration in your router
For downloading ngrok https://ngrok.com/ go to this link
For configuration your router it will take some searching in google based on what type of router your using
You must mention that your localhost or Nat Ip and your public IP to resolve here is NOIP refrence https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
As you specified that you want the backend to be private, such that it can only be accessed by your specified node. You will have to host this node server on a cloud service or you can host it on your local machine by opening a port for the node server. Suppose you host the node server on port 1234 on your local machine ip address.
You can start the node server on localhost and your desired port, but you need to allow requests to the particular port.
Now you need to check for the origin of the request that your node server receives and validate that, so that only your private node(computer) can access the node server. You can do that by validating the hostname using express, you can get the hostname of the request in express.js using req.hostname or req.headers.host.
You would need to use express.js for this functionality and the code would be as follows
let express = require('express');
let app = express();
let allowedHost = 134.32.234.3 // the hostname which is allowed to access the backend
let port = 1234; // desired port
let host = 0.0.0.0 // desired host; 0.0.0.0 to host on your ip
app.get((req, res) => {
res.header('Content-Type', 'text/html');
if(req.hostname == allowedHost){
res.send('<html><body><h1>Hello World</h1></body></html>');
}
else{
res.send('Connection not allowed');
}
});
app.listen(host, port, ()=>{
console.log(`The server is running on http://${host}:${port}`);
}
i am trying to url in node.js project
currently show in my local server port like -http://localhost:1337
expected url =http://localhost:1337/nodeprovider
var http = require('http');
var port = process.env.port || 1337;
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port);
If I understood you right: you need your node.js server to response on http://abcwebsite
/nodeprovider
Since this that task must be done not in Node.js server I suggest you to read about domain parking: https://en.wikipedia.org/wiki/Domain_parking
Making long thing short: Node.js server is not responsible for domain name it responds on. Inside server script you can only set port to listen.
From an old answer
You dont assign a domain to a node.js server, instead you load your app onto a machine which has an ip adress. You then need to make sure your app is listening on the correct port, which on most servers is 80.
now getting a domain name to point to this ip adress is an entirely separate matter. You need to make your name server point to the ip. Your name server will usually be the company you bought the domain name through, for instance GoDaddy is a Domain Name Server (DNS). So if you had a domain name with them, you would go on their site under DNS settings and change the ip adress. Your domain name will then point to your ip adress and should render your node.js app.
i have a project where users connect to my router, and then enter the address http://192.168.1.50:9091 into the address bar to connect to a page. I would like there to be a way to enter something easier than this long ip address.
here is what's happening so far:
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
server.listen(9093);
var nRequest = 0;
var nConnexs = 0;
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client_app.html');
});
app.get('/2', function (req, res) { res.sendfile(__dirname +
'/client_app2.html'); });
I am not sure what you mean by:
i have a project where users connect to my router
The Node.js application is sitting on your machine. Until somebody accesses your machine (through IP), the application is never accessed.
As such, you must put something between the user and the application that will direct the user to the IP. For real websites, this is accomplished using DNS. You register a domain name and tell the DNS service the IP of the machine to redirect to.
If you have control over the router (internal network, etc.), you can map an alias to your IP address. It all depends on whether your router runs a DNS server or supports DNSMasq. You will need to check your router manufacturer/model/etc.
Finally, if there are a small number of users that will be accessing your website, you could always have them use the hosts file to map a name to the IP. The location of this file is dependent upon the operating system; just Google: edit hosts file
I assume users are initially connecting to your router's public ip address or a DNS representation of that. If that's the case, you can usually configure your router to do port forwarding such that incoming requests on a particular port are automatically routed to a particular private IP address on your network. In that case, the users would connect to your router, but that request would be automatically forwarded to your internal node server and thus the users would end up directly connected to your node server.
This type of configuration comes with the usual security warnings. Doing this means that your node server must be properly configured against internet attack and your node app must be written with appropriately security precautions - particularly because any compromise to the computer the node server is running on has access to your internal network.
If you show us what users are initially connecting to when they connect to your router, we could offer a more explicit example of how you could configure the router to take advantage of port forwarding.
I'm running a node app on an Amazon EC2. The app includes a simple web server intended to serve the index page, but it doesn't work.
Here's the server code:
var http = require('http'),
fs = require('fs'),
io = require('socket.io'),
index;
fs.readFile('client.html', function(err, data){
if (err){
throw err;
}
index = data;
});
var server = http.createServer(function(request, response){
response.writeHeader(200,{"Content-Type": "text/html"});
response.write(index);
response.end();
}).listen(1223);
The EC2 is assigned the public IP address 54.187.31.42. I run the app, open my browser, connect to 54.187.31.42:1223 expecting to be served a web page and get nothing.
What is wrong with my server code?
I used the snippet found in this answer to check the IP of the EC2 running the app, and oddly get 172.31.3.67 - why is the returned address different from the one assigned to the machine by Amazon?
Consequently, trying to connect to 172.31.3.67:1223 also fails.
Straight from the Amazon dev controls, if that helps confirm it isn't an issue of the server IP being wrong or something.
The code looks fine, try connecting with the public IP/public DNS that you see in the AWS console.
Try the following and your application would work:
Open the port (in your case 1223) in the security groups of your instance.
stop the firewall on your machine (i.e. iptables) and now access your server using public ip or public DNS.
If you can now acceess your machine that means something in the iptables is filtering your traffic. You can modify the iptables rules accordingly.
In security group, add a rule with the type "Custom TCP Rule" on the used port (e.g. port: 3000 or 1223 for this case). It works for me.
So I'm installing node.js on amazon ec2 with ubuntu 8.04, and and have run node sayhello.js which is this code:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<br/><strong> Hello World!</strong>');
res.end();
sys.puts(sys.inspect(req, false));
}, 2000);
}).listen(8000);
sys.puts('Server running at http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/');
I see
Server running at
http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/
being displayed in the console correctly.
The tutorial says: go to :8000 in the browser and you should see Hello World!
I go to http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/ (not the real address) but it doesn't load (just connecting...). The example uses localhost, is doing the public domain incorrect or some such?
Thanks.
You need to open up port 8000 in your security group.
If you've got the EC2 command line tools installed, try running:
$ ec2-authorize default -p 8000
This assumes that you're using the default security group. If not, change default to the name of your security group.
If you're just using the web interface follow these steps:
Login to the AWS console
Select Amazon EC2 in the top bar
Click on Security Groups in the menu on the left
Click on the security group that you assigned to your EC2 instance (probably just default)
In the bottom window pane, click on the Inbound tab
Set Port range: to 8000 and leave the other two inputs as they are
Click Add Rule
Within the security group associated with the EC2 instance, make sure you have port 8000 open to your IP or to the public.
Check the following:
That you are allowing access from all ips to port 8000 in the security group
That you have modified the CORRECT security group (e.g., quicklaunch-1) and not, say, quicklaunch-2. I've edited the wrong one more times than I care to admit.
That you have opened port 8000 on the Linux firewall
That your server is actually running (you should see "Listening on port 8000" in the command line).
Here is a tutorial on how to set up a Node.js web server on Amazon EC2: http://www.lauradhamilton.com/how-to-set-up-a-nodejs-web-server-on-amazon-ec2
It's a bit more complicated than what you're doing (because it uses ip forwarding from 80 to 8080) but the part about opening the Linux firewall is the same.