Start Node.js server on Digital Ocean - javascript

I have created a Node.js Droplet on Digital Ocean.
I have connected to the server with FileZilla and uploaded all the Node.js files (index.js, package.json, etc.).
I am trying to start the server with node index.js. It says
App is running at http://localhost:3000 in development mode
Press CTRL-C to stop
However, I am not able to see the website neither on the public IP address provided by Digital Ocean nor on localhost:3000.
How do I start the Node.js server correctly?
Edit
I am setting up the server with this (content of index.js):
const express = require('express');
const app = express();
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), () => {
console.log('App is running at http://localhost:%d in %s mode', app.get('port'), app.get('env'));
console.log(' Press CTRL-C to stop\n');
});

First you want to ensure everything is running fine by opening another terminal on the same machine and executing a curl.
curl http://localhost:3000
After you have confirmed that is working, you will have to open the appropriate ports on the machine.
Let's see what ports are currently open by running the below command.
sudo ufw status verbose
If port 3000 isn't listed, lets add it with the following command.
sudo ufw allow 3000/tcp
Verify port 3000 has been added by running ufw status again.
sudo ufw status verbose

Related

Node js and socket.io not have any error but not work when access by url, how can i do?

Node js and socket.io not have any error but not work when access by url, how can i do ?
My step to install is
cd /home/admin/web/my-doamin-name/public_html
npm init
npm install --save express socket.io
node app.js
...............................................................
After call node app.js, it's will show
start server on port :3000
...........................................................
app.js
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendfile('index.php');
});
http.listen(3000, function(){
console.log('start server on port :3000');
});
.......................................................
index.html
<html>
<head></head>
<body>HELLO WORLD</body>
</html>
.......................................................
When i test telnet on port 3000 by this command line
telnet my-domain-name.com 3000
It's show like this.
Trying xxx.xx.x.xxx...
telnet: connect to address xxx.xx.x.xxx: Connection refused
.......................................................
And i test port 3000 by this command line
ss -nltp|grep :3000
But it's not show anything.
.......................................................
And then i tried to access to mysite (on google chrome)
https://www.my-domain-name.com:3000
In google chrome it's show error
This site can’t be reached
How can i do ?

Run React application in vm azure

I want to run my react application on azure's virtual machine. I started my application in azure's vm but when I go to public url (DNS) of vm it says site can't be reached.
This is what I did to start my project in vm
I ran command ssh a********#52.*..***
After entering vm, I cloned the git project (React nodejs project)
I then install all the packages like npm, node and yarn, important to my project
I then ran gulp command. My project gets compiled successfully
Now, I tried accessing public url (DNS) of my azure's vm
Expected Result: I should be able to see my application page when DNS accessed
Result: Site cannot be reached
This is the serving part of my server file in project
const PORT = (process.env.PORT || 4200);
const runServer = (app)=> {
app.listen(PORT, ()=> {
debug(`Listening on http://localhost:${PORT}`);
if (__DEV__)
debug(`HMR on http://localhost:${config.BS_PORT}`);
});
}
runServer(app);
I started my application in azure's vm but when I go to public url
(DNS) of vm it says site can't be reached.
Can you access this site with localhost? Please test it in your Azure VM, to make sure this service running in the correct port.
We should add port 4200 to Azure NSG (Network security groups) inbound rules.
We can follow this article to add port to NSG inbound rules.
By the way, if you are create a classic VM, we should follow this article to add port 4200 to VM's endpoints.
Also we should check Linux firewall settings, make sure the firewall will not block port 4200.
Finally, we can use telnet to test the network connection.
Update:
We can use netstat -ant | grep 4200 to check the service is running or not.
For example, SSH service listen on port 22:
jason#jasonvm:~$ netstat -ant | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 104 10.0.0.4:22 167.220.255.8:57001 ESTABLISHED
tcp 0 0 10.0.0.4:58122 52.239.153.36:443 TIME_WAIT
tcp 0 0 10.0.0.4:40922 168.63.129.16:80 TIME_WAIT
tcp6 0 0 :::22 :::* LISTEN
Also we can use telnet to test it, we can install telnet on your local PC, and run this command in CMD, like this telnet vmpublicIP 4200.
If your service listen on port 4200 and it run correctly, we can use this command to access it:
curl localhost:4200

Unable to connect from a different network express.js

I'm experimenting with node.js and express.js.
When I try to connect to my web server from any computer in my network, it works, but then when I try to connect from outside network the connection times out.
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res) {
res.send("Hello World");
});
http.listen(3000, '0.0.0.0', function() {
console.log("Listening on port 3000!");
});
I just tested your code and I'm able to access the server from outside my local network by navigating to:
http://173.0.[my].[ip]:3000
So the code is correct. It could be that you need to open the port 3000 to the outside world. Here's how it can be accomplished.
Through your router admin interface
Here's mine for example:
Where 192.168.1.130 is the local IP of the PC I'm running the http server on.
Don't forget to click the Save settings button in that interface to apply the changes.
Using a tool like ngrok (mentioned by eddiezane)
Install ngrok through their website or without leaving the command prompt, with the ngrok node wrapper.
npm install ngrok -g
Start your http server and then run:
ngrok http 3000
Navigate to one of the url in front of Forwarding:
The free version is more for a quick test and less as a definitive way to expose a service in a production environnement since every time you restart ngrok, a new user-hostile url is given to you.
Other possible problems
It could also be that you need to add an exception to the firewall (if on windows).
To add to Emile's answer, I would check out ngrok which is an awesome tool that generates you a publicly accessible URL for a port on your local machine.
Here's a good blog post on it my buddy wrote.

Can't get socket.io to work on server, while running fine locally

This code is running fine with the node.js server running locally:
Server:
var client = require('socket.io').listen(3001).sockets;
Client HTML:
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
Client Javascript:
var socket = io.connect('http://localhost:3001');
Then I moved the server.js to my ubuntu server which has node and socket io installed, using the same code, and started it without error.
Clientside I changed the local html/javascript file from localhost:3001 to ServerIP:3001 and it just doesn't work anymore. When I check firebug, I can see it never finishes trying to GET http://ServerIP:3001/socket.io/socket.io.js. It does not fail, just never finishes.
What did I do wrong? Thank you.
This is most likely a firewall issue.
Allow connections on port 3001 using the following (if you use iptables)
iptables -A INPUT -p tcp --destination-port 3001 -J ACCEPT
Than try again

How to access remote node.js app in browser, not on localhost

I'm trying to set up a simple "Hello world" node.js app.
I've created the following index.js file:
var app = require("express")();
var http = require("http").Server(app);
app.get("/", function(req, res){
res.send("<h1>Hello worlddddd</h1>");
});
http.listen(8080, function(){
console.log("listening on *:8080");
});
When I open up my local console, and perform node index.js, I get the message "listening on *:8080", as expected. I point my browser to localhost:8080, and I see the HTML page saying "Hello worlddd", as desired.
Now, I'm trying to do the same on my Virtual Private Server, so I can access the same app from different computers, but all I get is connection timeouts. I've followed these steps:
Install node.js on my VPS
Install express via npm install --save express#4.10.2
Upload my index.js file to the var/www/html folder on my server with IP 192.123.123.12 (an example, this isn't my real IP).
Access the server via PuTTY, and run node index.js, where I get "listening on *:8080", so I know node.js is working.
Now I point my browser to http://192.123.123.12:8080 and after about 20 seconds, I get the browser error: "The connection has timed out".
I've tried listening to port :80 instead, but I get the error that this port is already in use.
Does anybody know what I'm doing wrong? Am I using the wrong port? Am I pointing to the wrong URL? Do I need to modify my server preferences? (running Apache on CentOS). I've only found dozens of tutorials that teach you how to run a node.js app on your local computer(pointing the browser at localhost:8080), but I need it to run on my remote server so multiple computers can access the same app.
The issue is that your current filters (iptables) block traffic unless you explicitly allow it.
You just need to open port TCP 8080 inbound, and you should be able to reach your node.js server!

Categories

Resources