Azure web deployment not displaying web page - node.js

I'm writing a simple nodejs app to be deployed to azure.
The app works fine, 100%, but the web page I have to manage admin matters refuses to load.
It always displays internal server error. I'm using express and viewing the logs but they say nothing useful. The app doesn't crash so I cant understand why it wont display.
This is the simple hello world code I'm using for testing, and even that wont display.
var express = require('express');
var app = express();
app.use(express.static('website'));
app.listen(80);
Does anyone have any idea what could be the problem?
Edit: I forgot to include that I do set the port environment variable
var port = process.env.PORT || 80;
var baseHost = process.env.WEBSITE_HOSTNAME || 'localhost';
Thats at the top of my server.js, sorry about that.

guess your app is listening on the wrong port when running on Azure App Service.
you will have to get the port number from environment variable "process.env.port"
var app = require('express')();
var port = process.env.port || 8080; // 8080 for local or whatever number u want
var listener = app.listen(port, function(){
console.log('Listening on port ' + port);
});

Follow this blog to get a Node.js app working on Azure. I used this for a demo recently and it worked properly.
Create a Node.js web app in Azure App Service
You have to use GIT and Continuous Deployment to get Azure App Services to recognize and setup the right pieces for your node application to function, it also takes care of node packages for you automatically.
Hope this helps, TehDude

Try turning on logging to get a better idea of what's going on
From How to debug a Node.js web app in Azure App Service:
To enable the logging of stdout and stderr streams, you must create an
IISNode.yml file at the root of your Node.js application and add the
following:
loggingEnabled: true
This enables the logging of stderr and stdout from your Node.js
application.
The IISNode.yml file can also be used to control whether friendly
errors or developer errors are returned to the browser when a failure
occurs. To enable developer errors, add the following line to the
IISNode.yml file:
devErrorsEnabled: true
Once this option is enabled, IISNode will return the last 64K of
information sent to stderr instead of a friendly error such as "an
internal server error occurred".

Node.js application running on Azure Web Apps Service, is hosted on IIS handled mapping via IISNode, which gives a Named Pipe to receive the incoming requests, not a TCP port like you would use when running locally.
This Named Pipe has been defined as the port in Node.js runtime on Azure Web Apps. You can define the port in your app like: process.env.PORT || 3000, with which your app can run on Azure or locally.

Related

Azure App Service - Node App - Do i need to createServer and required files

I have a node app, that doesn't expose any port. It's just running some tasks in the background and returning some stuff onto the console that I occasionally need to look at.
I've deployed this onto Azure App Service - however it doesn't seem to run - in the logs I see
Waiting for response to warmup request for container xxx
I was wondering in my index.js I don't actually expose any port - namely there isn't a const server = http.createServer(). Is this required from Azure's side or can I disable it?
So my index.js literally looks like:
(async () => {
// Check some things and do something
}
Also, in the https://github.com/Azure-Samples/nodejs-docs-hello-world sample project there are the files:
web.config
process.json
Do I need these for the node app to run on Azure?
And finally, in teh sample node app above, it has const port = process.env.PORT || 1337; but where in Azure's portal is that being set? Or if its defaulting to 1337, how does Azure know that?
Any help appreciated.
Thanks.
If you just want to run some tasks in the background, you can use webjob instead. It is also very convenient to check the output.
By the way, the only ports open for Web Apps are 80 and 443. We should use process.env.PORT for nodejs app port. 1337 is for local test.

Google Cloud Compute Engine - Nodejs not working

I created a free server from Google Cloud. I want to run Node.js on this server. I installed Node.js. I installed Express and created a project. Then I run the project (working).
But I can not get the output by typing ip-address: 3000. "This site can not be reached." I get as a result.
What could be the reason for that.
I just setup one to see how this works, how i setup the server, hope this helps:
var express = require('express');
var app = express();
app.get('/', (req, res) => {
res.json('Home Page');
})
app.listen(8080);
The web applications must listen for HTTP requests on ports within the permitted range 8080 to 8084.....To connect to a web application running on an instance, click the Web Preview button Web Preview Button above the Cloud Shell terminal window in the GCP Console. src: https://cloud.google.com/shell/docs/features#web_preview

Google App Engine 502 (Bad Gateway) with NodeJS

I have a full web application using NodeJS, MongoDB (Mongoose as the driver) and ExpressJS.
The project works perfectly on my local machine. Today I decided to move everything to production. I'm using Google App Engine to host my application, and Compose (formally MongoHQ) to host my database.
App Engine servers my application perfectly, although my API does not seem to work. My API is served from example.com/api, and each request (GET, POST, DELETE and PUT) all returns a 502 (Bad Gateway) error.
I tried running my application on my local machine while connected to my remote MongoDB database and that worked perfectly fine. So it must be a problem with App Engine or NodeJS, not with MongoDB.
I have tried checking all error logs within Google Cloud, although there are no errors.
Why is App Engine/NodeJS serving my application's static content perfectly fine, although not allowing any requests to my API?
just make sure that your server listens on 8080 port
https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build#listen_to_port_8080
502 Bad Gateway is usually an error on the Nginx side. Unfortunately those logs are not surfaced to Cloud Logging, yet.
A lot of times the problem is that your HTTP packets are too big for the buffers or something similar. The way you can see the nginx log is something like this:
Use just 1 VM. This isn't strictly necessary, but a lot of times it makes it easier to debug your application if you know that your requests on the one machine. You can accomplish this by adding this to your app.yaml:
manual_scaling:
instances: 1
then re-deploy
Switch the VM from "Google owned" to self-managed. This can be done in the Cloud Console. Go to Compute Engine, instances, click on the instance name that matches the App Engine version, and you should see an option to switch it to self-managed.
gcloud compute ssh <instance name> to SSH to the machine
docker ps to see your running containers. Look for the container named nginx and grab its id.
Once you have a container ID, you should be able to docker exec -it <container id> -- cat /var/log/nginx/error.log. You might want to ls that whole log directory.
You will likely see an error there which will be a bigger hint as to what's going wrong.
I know this is way more complicated than it should be :-\ If you have any problems with the steps above, leave a comment. If you do find an error and you're not sure what to do with it, also leave a comment.
I had the same problem, I was getting "nginx 502 bad gateway" error on GAE standard environment. There are many reasons for this but I finally got it working. Try these:
1) Run the app on the correct port. Google will set the PORT environment variable. I was running on port 8080, in the stackdriver logs I was getting this warning:
App is listening on port 8080. We recommend your app listen on the
port defined by the PORT environment variable to take advantage of an
NGINX layer on port 8080.
The code below gets the port from environment, if PORT is set otherwise defaults to 8080:
const PORT = process.env.PORT || 8080;
2) Go to google cloud console -> logging -> logs viewer. Select Google App Engine and then your service from the down and check you logs. Are you getting the requests at all or does it look like the requests do not react to your server. In my case, I was not getting them even after I fixed the port:
2020-03-02 21:50:07 backend[20200302t232314] Server listening on port
8081! 2020-03-02 21:50:08 backend[20200302t232314] "GET /create-user
HTTP/1.1" 502
Fix any error if it looks like your application is failing to start, throwing exceptions etc..
3) Don't pass an IP when you are running your server. It seems Google runs the app at a pre-defined IP address and do not want you to modify it:
server.listen(PORT);
4) Don't try to run on https! Google is running an nginx server in front of your app and it is handling the SSL and redirects to your app over http. You can use the environment variable NODE_ENV(it is set to "production" in GAE environment) to run on http on production and https elsewhere, like this:
let https = require('https');
let http = require('http');
if (process.env.NODE_ENV == "production") {
http.createServer(app).listen(PORT, function () {
console.log(`Server listening on port ${PORT}!`)
});
} else {
https.createServer({
key: fs.readFileSync('host.key'),
cert: fs.readFileSync('host.cert')
}, app).listen(PORT, function () {
console.log(`Server listening on port ${PORT}!`)
});
}
5) I didn't need to set any handlers in my yaml file, it might be causing errors for you if you have incorrect configuration. My yaml file is pretty straightforward:
runtime: nodejs12
env: standard
instance_class: F1

AZURE + SOCKET.IO: How do I know which port my Web App is using?

I went to a Hackathon last weekend and a Microsoft recruiter set me up with Azure for my Node.js project.
We used Socket.io with my project and had a hard time connecting the Client to the Server because we didn't know which port to connect to...
On our WebApp (not Azure VM), we had the following code:
var port = process.env.port || 3000;
On the client side of Socket.io, I had to specify an ip address to use along with it's port. I tried:
var socket = io('http://IP.AD.DRE.SSS:3000'); //And
var socket = io('http://IP.AD.DRE.SSS'); //And even a different Port
var socket = io('http://IP.AD.DRE.SSS:9999'); //And 443 and 80
And every iteration... I had to be doing something wrong. We ended up switching over to Digital Ocean because I knew how to use it but I really wanted to get this working.
Any Ideas?
UPDATE:
I changed it to 80 and my current error is: "Access Control Allow Origin." Note: My client is running on a server.
UPDATE 2: Return of the OP
Unfortunately, the CORS package for Node did not do the trick...
Some more info:
I'm not using Express or Connect. My server is on Azure (as an Azure Web App). My Client was on Localhost (Thanks to WebStorm).
Azure Web Apps only listens on ports 80 & 443. Change the port to either of them and your app will work fine.
Just leave the port off in the client string. You can connect using the URL alone. Look at my code I codefoster.com/commandmonkey as an example.

How to deploy a socket.io node.js application on windows azure?

I am building windows azure application which is primarily based on .NET, but I also have to build a socket.io server using node.js hence i need to deploy a socket.io server and use this socket.io url to connect in my .NET application.
I followed all the steps listed here . And I am able to get the socket.io running on my local but when i deploy to cloud, it doesnt start. Please find below a code snippet for socket.io
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server, { origins: '*:*' });
server.listen(4001);
When i hosted it in my local emulator, 127.0.0.1:81 was pointing to this in my browser
But 127.0.0.1:4001 showed "Cannot GET /" on the browser, which is an indicative that the socket.io server is running on that url.
But when i deploy the same to cloud, i get the same as the screenshot on the url where the cloud service is hosted but on port 4001 where the socket.io server should have started it says page cannot be displayed.
Please let me know if you need to see any other files like web.config etc.
I have been stuck on this issue from forever and its really crucial for my project, any suggestions or ideas would be deeply appreciated.
Thanks
The important part that you are missing from the sample is setting of the port number
var port = process.env.port || 1337;
and
.listen(port)
when you are running inside of the Azure environment (even emulated) the ports are assigned for you, the port environment variable will tell you where. 4001 is likely not the assigned port.
The 1337 would only be used if you are running by executing
node server.js
from the command line

Resources