Unable to connect from a different network express.js - javascript

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.

Related

AWS Cloud9 application not running on different ports

I'm developing a node.js application on AWS Cloud9 environment.I'm using express to run a local server.I have installed express version 4.17.1 and using node version 10.6.3.I want to run or preview my application on port 8081 or 8082 but it isn't previewing. Url i'm pasting is https://blablabla.vfs.cloud9.us-east-1.amazonaws.com:8081/ and it shows The web page at https://blablabla.vfs.cloud9.us-east-1.amazonaws.com:8081/ might be temporarily down or it may have moved permanently to a new web address.. Note- In past it used to run on different ports like 8081,8082.Take a look at this code which i'm using.
var express=require('express');
var app=express();
app.get("/",function(req,res){
res.send("cfg");
});
app.listen(8081,process.env.IP);
It is running on default port 8080 ie if we replace 8081 in above code with process.env.PORT, it shows the ouput "cfg" on browser.I know it's a easy one but please check this issue once, maybe there is some problem with my ec2 instance.

How can someone else connect to my Websocket server?

Using this module i'm making a websocket server on my pc.
Problem is: How do I let people connect into it? I know how to connect it from LAN(which is using ws:LANip:Port, for example:192.168.0.7:8000) but what if someone not connected in the same router, from another country(for example) wants to connect?
EDIT: And I tried using MyIP:Port but it doesn't seem to work.
Try ngrok or localtunnel. Both of them are free.
https://github.com/bubenshchykov/ngrok
https://github.com/localtunnel/localtunnel
To expose your local port 8000 for instance:
npm install ngrok -g
ngrok http 8000
When someone tries to connect to your external IP, they talk to your router. Your router needs to know which computer on its network to connect the external connection to. This is known as port forwarding. To find out how to do it, do a Google search for "<your router model> port forward", but first read about port forwarding in general to understand the concept itself thoroughly.

How to make Node.js listen to specific website and use AWS?

So I'm kind of new to Node.js but i really want to host a website that uses Node.js in the background using Amazon Web Services (AWS). I am using Socket.io and Express.js with Node, and i have a html file with the client side code.
Here's part of each file:
server.js:
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
io.on("connection", function(socket) {
console.log("-- User Connected");
});
//express home page
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
//express listen on 8080
http.listen(8080, function() {
console.log("Running...\nListening on port 8080");
});
index.html (Just the client-side javascript)
<script src = "/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
var socket = io.connect();
socket.on("connect", function() {
console.log("connected");
});
});
</script>
Everything works great, but i was wondering how i would upload this to a AWS bucket and run it there. I already uploaded the full .html file to a AWS bucket and set up the host, so it opens and runs fine. But how would i go about uploading and running the server.js file? and what would i change in both the client side code (change io.connect() parameters?) and the server.js code (change .listen() to something?) so it runs with AWS?
Any help is much appreciated, thank you!
Buckets are a feature of AWS' simple storage. They only support static files. You can't use them run server side side programs that you wrote yourself.
For that you'll need a different product, such as EC2.
You can run Linux on Amazon EC2 instance.
Guide to get started with Amazon EC2.
Step 1: Create a Github/Bitbucket repository of your project so it can be easily cloned on the server. Private repo in GitHub are paid while in Bitbucket it's free under some conditions.
Step 2: SSH into the server. Clone the project. Install the required packages. Now you can run the node server on EC2 instance as you do on your localhost.
Step 3: AWS provides you with public DNS something like: ec2-**-**-**-**.compute-1.amazonaws.com Now access node server through ec2-52-86-163-5.compute-1.amazonaws.com:3000/
Step 4: To run the node app continuously you need something like forever
You can only use S3 for hosting static websites as described in this example.
If you would like to host your Node.js application on AWS I recommend that you use Elastic Beanstalk as explained in Deploying Node.js Applications to AWS Elastic Beanstalk. The main difference compared to hosting the Node.js application on EC2 is that Beanstalk is a service that provides a runtime environment, i.e. you do not have to set up and manage the operatings system yourself. All you need to do is to package your application and upload it to Beanstalk. Consequently, a launch environment will be created and configured with the AWS resources needed to run your code.
For more information, please read What Is AWS Elastic Beanstalk?

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!

how to run node.js on windows with apache server installed in?

I'm a node.js begginer . Let's say I have an apache server(XAAMP) and node.js installed in C:\Program Files\nodejs\nodejs.exe on windows 7.
How can I run node.js in my apache server to simulate my code?
I mean, I know how to write node.js code but what I don't know how it's work on my server?
Apache server don't need for Node.js.
For create your own Node.js server:
Download and install Node.js
Create file hello.js:
var http = require("http");
var server = http.createServer().listen(3000); // beter way for create
server.on("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
// for view at page http://localhost:3000
res.write("Hello world");
res.end();
});
server.on("listening", function(){
// for view in console
console.log("Listen: 3000...");
});
In terminal go to dir where file hello.js and type:
node hello.js
Open your browser and point it at http://localhost:3000/. This should display a web page that says:
Hello world
A basic HTTP server
Node.js Manual & Documentation
If you like to work with a replacement for XAAMP you should finally take a look at MEAN.io.
At NpmJS.org you will find different solutions for most of your needs.
and like Reagan Gallant commented you should take a look at this famous stackoverflow post (if you need ideas).
NodeSchool indeed is a good entry point for your fist steps. After that npmjs will make sense and finally you will love Mean.io
You just make it use a different port than Apache is using (for example port 3000 which is the default for express-js and others) -- that is assuming that you don't need the two to work together.
If you do need them to work together, you add a forwarding module to Apache and configure the forwarding in Apache of certain URL to go to your local port for node-js

Categories

Resources