PeerJS Server 404 on Azure - node.js

I'm trying to deploy a PeerJS server on Azure. On my kudu console, running
node peerjs --port 9000
returns
Started PeerServer on ::, port: 9000, path: / (v. 0.2.8)
However, when I try to connect to the server from my client code, I get a 404. Going directly to appname.azurewebsites.net/peerjs/id in my browser also returns a 404.
I see inside their package.json file, they run
bin/peerjs --port ${PORT:=9000}
instead of just passing in 9000 directly; I assume this is an environment variable. However, trying to run this on Azure gives
Error: Error: listen EACCES ${PORT:=9000}
which I assume means Azure doesn't recognize ${PORT:=9000} as a valid port.
I know for a fact there's nothing wrong with my client side code because a) I copied it directly from PeerJS's website, and b) everything works correctly when I deployed PeerJS to Heroku. It's only not running on Azure.
Other things I've tried: I edited peerjs in the bin folder to use process.env.PORT instead of what's passed in via the command line, but that didn't work, giving the same EACCES error. When I tried to console.log(process.env.PORT), I got undefined. None of my Google searches have turned up any solutions, although this person (Custom PeerJs Server giving ERR_CONNECTION_TIMED_OUT) seems to have a similar error, not on Azure.

Azure App Service doesn't allow us to listen on a customer port. We need to use process.env.PORT instead. See Listen additional port Microsoft Azure Nodejs.
Azure App Service (on Windows platform) runs on Microsoft IIS. So we need to put the app files to its virtual directory (D:\home\site\wwwroot) and no longer need to manually run the app via the Kudu console.
In this case, you first need to install the library under app's root:
npm install peer
And then create a file named index.js or app.js with following content and put it to /wwwroot folder:
var PeerServer = require('peer').PeerServer;
var server = PeerServer({port: process.env.PORT, path: '/'});
As #Mikkel mentioned in a comment, PeerServer uses WebSocket protocol, so Web Sockets should be enabled in the Azure portal like this:
You also need to check out this post to add a web.config file for your app if it has not been created yet.

This will be a firewall problem... You will need to open port 9000 in your Azure settings panel.
From the machine itself, open up a browser to http://localhost:9000/ or http://localhost:9000/peerjs and you should see the standard Peerjs server JSON output.
Or if you only have command line, try curl http://localhost:9000/ or http://localhost:9000/peerjs

Related

nginx.conf for NodeJS/React App returning 502 and 405

Trying to setup a staging environment on Amazon LINUX EC2 instance and migrate from Heroku.
My repository has two folders:
Web
API
Our frontend and backend are running on the same port in deployment
In dev, these are run on separate ports and all requests from WEB and proxied to API
(for ex. WEB runs on PORT 3000 and API runs on PORT 3001. Have a proxy set up in the package.json file in WEB/)
Currently the application deployment works like this:
Build Web/ for distribution
Copy build/ to API folder
Deploy to Heroku with web npm start
In prod, we only deploy API folder with the WEB build/
Current nginx.conf looks like this
Commented out all other attempts
Also using PM2 to run the thread like so
$ sudo pm2 bin/www
Current thread running like so:
pm2 log
This is running on PORT 3000 on the EC2 instance
Going to the public IPv4 DNS for instance brings me to the login, which it's getting from the /build folder but none of the login methods (or any API calls) are working.
502 response example
I have tried a lot of different configurations. Set up the proxy_pass to port 3000 since thats where the Node process is running.
The only response codes I get are 405 Not Allowed and 502 Bad Gateway
Please let me know if there is any other information I can provide to find the solution.
It looks like you don't have an upstream block in your configuration. Looks like you're trying to use proxy-pass to send to a named server and port instead of a defined upstream. There's is an example on this page that shows how you define the upstream and then send traffic to it. https://nginx.org/en/docs/http/ngx_http_upstream_module.html
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}````
Turns out there was an issue with express-sessions being stored in Postgres.
This led me to retest the connection strings and I found out that I kept receiving the following error:
connect ECONNREFUSED 127.0.0.1:5432
I did have a .env file holding the env variables and they were not being read by pm2.
So I added this line to app.js:
const path = require("path");
require('dotenv').config({ path: path.join(__dirname, '.env') });
then restarted the app with pm2 with the following command:
$ pm2 restart /bin/www --update-env

My Ionic app can't access my local Node server from Android emulator

I'm trying to run for the first time my React/Ionic app on an Android Emulator using Capacitor. The app should connect to my local Node server using Axios.
While my app started successfully on the emulator, all server requests failed with Msg: Error: Network Error
Here's the steps that I did:
I configured Axios to connect to http://10.0.2.2:3001 instead of localhost, but the requests still fail
I opened the url http://10.0.2.2:3001/api in the Android Emulator Chrome browser, and it succeeded in connecting to my server and returning a response
I tried adding to my capacitor.config.json file the following conf, but my app still fails
"server": {
"allowNavigation": ["10.0.2.2"]
}
Would you have other advices I should try to make my app connect to my local server? Thank you
After searching, I found the solution in another SO question :
The problem comes from the fact the my app uses http instead of https requests which Android forbids by default, starting with API level 28.
To allow http requests, I added android:usesCleartextTraffic="true" to my app's AndroidManifest.xml
Reference: https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic
if you use instances on cloud, modify host file, on linux the rute is : /etc/hosts
add this line:
localhost < ip-public-your-instance >

How does a react app can be set up on server

I'm trying to understand what needs to be done to put my react app online.
Until now, I launched it on my mac using npm start, and accessing localhost:3000 or http://127.0.0.1:3000.
So I currently have bought a small server, installed everything (last version of node and npm, git and other necessary things), cloned my repo, and installed all dependencies.
When I do npm start on the server, it says it's available on port 3000. But when I go in my server's ip with the following :3000, it times out.
I don't really understand what need to be done to do this, I found some things about configuring apache on the server, others about using pm2 so have a node script running even after leaving the terminal, but that would be my next step I guess.. And other about configuring things with express (but do I need node+ express here ? As it's a simple front end react page ?).
if you are using webpack devserver, use it for development only
The tools in this guide are only meant for development, please avoid using them in production!
back to your question, there is a difference between binding to 127.0.0.1 or binding to 0.0.0.0
try changing the devserver to listen to 0.0.0.0
webpack.config.js
module.exports = {
//...
devServer: {
host: '0.0.0.0'
}
};
Usage via the CLI
webpack-dev-server --host 0.0.0.0
also note, that you will need to allow ingress rules (incoming connections). that is, allow a request from the internet to reach your server
There are a lot of configurations you will have to do when you deploy your application on a server. Building the app, Nginx, pm2 and even ssl certification. This video is 20min and has all you need. https://www.youtube.com/watch?v=oykl1Ih9pMg&t=1s

502 Bad Gateway with nginx | Google App Engine | Node JS

I am hosting the web app on Google Cloud Platform with App Engine and I am using ExpressJS and MongoDB, which is hosted on mLab.
Everything worked well until 1/1/2017. I had vm:true before and now was forced to change the env to flex. Now I am getting 502 bad gateway error with nginx. App engine doesn't allow us to change the nginx config file.
I had tried the suggestion from this post: Google App Engine 502 (Bad Gateway) with NodeJS but still doesn't work.
For some reason, I have another app with exactly the same setting on app engine and it works perfectly.
Any suggestion will be greatly appreciated. Thank you.
app should always listen to port 8080, google forwards all request from 80 to 8080
https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#listen_to_port_8080
check out the logs for any deployment errors
$ gcloud app logs read
I have came across a similar issue with the code provided by this tutorial (https://cloud.google.com/nodejs/getting-started/authenticate-users)
And found there was a missing dependency. I fixed the missing dependency and the app is deployed and working fine.
Details into the issue: https://github.com/GoogleCloudPlatform/nodejs-getting-started/issues/106
I had the same problem with Express. What solved it for me was to not provide an IP address for the app.
So my old code would be:
var ip = "127.0.0.1";
var port = "8080";
var server = http.createServer(app);
server.listen(port, ip);
This would result in a 502 in app engine.
Removing the ip was the solution for me.
server.listen(port);
Set the host to 0.0.0.0
Port 8080 is set by default by the engine. In fact, you are not able to define the environment var PORT as it is reserved.
Run the next command (as mentioned by #sravan )
gcloud app logs read tail
and make sure it looks like this,
[Sun May 27 2018 10:32:44 GMT+0000 (UTC)] serving app on 0.0.0.0:8080
Cheers
Google App Engine uses an nginx front to load balance all requests for node.js apps. With nginx acting as a forward proxy, this error usually happens when the request the user is making in the browser is reaching nginx (you see the unstyled 502 bad gateway error page) but the nginx server is not able to correctly forward the request to your node app. There could be many issues why this is happening but here are some common ones:
By default, App Engine assumes your node app is running on 8080. nginx itself will run on 80 and forward the request to 8080. Check if your app's port number is 8080.
You app may have a hostname defined like a domain something.appspot.com or an IP 127.18.21.21 or the like. Remove any hostnames from your server.listen or config.json or vhost wherever. App Engine will take care of domains, IPs etc so you dont have to.
Your app may be crashing before its sending a response to nginx. Check the logs of both nginx AND your node app.
To check logs / find out what is going on use this guide https://cloud.google.com/appengine/docs/flexible/nodejs/debugging-an-instance#connecting_to_the_instance to SSH directly inside the VM behind app engine. There will be one docker process with nginx where you can see the nginx error log and one docker image with your node app to check your node app's error message.
I'm just wondering, based on the activity in this question and the timestamps, why hasn't Google updated its documentation to cover this issue!!! ???
Please take care of http also, while deploying, it should be http server not https
var server;
if (process.env.NODE_ENV == "dev") {
server = https.createServer(httpsOptions, app);
} else {
server = http.createServer(app);
}
A 502 is not necessarily an error with nginx itself, it can most often happen when the nginx proxy cannot talk to your app container (usually because your app failed to start). If you get a 502 after migrating to 'env: flex' this is most likely due to some code changes needed in your app as mentioned in Upgrading to the Latest App Engine Flexible Environment Release.
Checking your application logs for errors from NPM will also help to diagnose the exact reason for the failed startup.
Create a server and then check with a ternary condition if current environment is production or not, assign port '80' if current environment is development else assign process.env.NODE.ENV.
const app = require('express')();
const server = require('http').Server(app);
const port = process.env.NODE_ENV === 'production' ? process.env.PORT :'80';
server.listen(port, ()=> {
console.log('listening on port number *:' + server.address().port);
});
In my case, I had the same error due to google app engine update which trigged auto re-deployment of my React SPA to the google cloud vm. Then it leads to a build fail in the process because of incompatibility of runtime which is node 16.x.x. Compatible runtime was node 14.19.0. I had to specify node version in my package.json file and do the deployment again to fix 502 Bad Gateway error.
{
"engines": {
"node": "14.19.0"
}
}
Also refer:
https://cloud.google.com/appengine/docs/nodejs
https://cloud.google.com/appengine/docs/flexible/nodejs/runtime
Hope this helps with someone having this issue with React SPAs.

Running Node App on Shared Hosting (It's almost working)

I've managed to get Node and NPM installed on my shared hosting account with Namecheap by following this answer. It all seems to be working, I can launch my app and it stays launched, but the subdomain which points to app's root route just shows up the folder of the domain.
I've tried going to myaddress.com/subdomain:8080 but I get a server error plus a 404. I've also tried changing Node's listening port from 8080 to 80, but on 80 I get an error:
Error: listen EACCES 0.0.0.0:80
So that means I don't have rights to port 80. How can I get clients to connect port 8080?
This is the part that shared hosting has a problem with, they wont open any ports for you. This is exactly where your workaround needs to be centered. Im currently looking for solutions to either use php to serve a node
see here https://www.npmjs.com/package/node-php-server
and here How to host a Node.Js application in shared hosting
or just configuring express to use somehow port 80.
These would be your best options.

Resources