Google Cloud Compute Engine - Nodejs not working - node.js

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

Related

How to connect my front end to a NodeJS Backend that's deployed publicly and not on localhost

I can connect them both when my NodeJS server is deployed on localhost PORT, for example
const PORT = 9000;
const app = express()
app.listen(PORT, () => console.log(`Server is running successfully on PORT ${PORT}`))
app.use(bodyParser.json({extended: true}))
app.use(cors())
app.use(bodyParser.urlencoded({extended: true}))
app.use('/', router)
And in my front end, I can do the following:
const url = 'http://localhost:9000'
...
const res = await axios.get(`${url}/post/${path}`)
This is just an example.
But what if I wanted to deploy my NodeJS server into a heroku application, for example randomname.herokuapp.com, and I want to do
const url = 'http://randomname.herokuapp.com:9000'
...
const res = await axios.get(`${url}/post/${path}`)
It obviously doesn't work. So I'd appreciate anyone who can help me do this.
Hello first of all if you deploy an app on heroku you will have to change port,you should add this to your code
app.listen(process.env.PORT || 3000,function(){
console.log("Server started at ${PORT}");
});
because heroku will not deploy on the same port as you did on your localhost.
Furthermore if i understand your question to link front with back end,in your code you should start building paths like
app.get("/")
app.post("/")
to handle get and post request to your home root and any other root.
Also in your front pages you need to take user input,if you want text field,checkbox or something else in a form and take them in your back end ,if it is on your home page for example.
app.post("/home",function(req,res){
const input=req.body.name;
}
I have also deployed apps on heroku and the main idea still remains same,you handle get,post request with these commands.You only change port and Pocfile to run your app on heroku.Get and post routes still remain same ("/home ,/info").
The link for your app is going to be:
"https://something_given_from_heroku.com" ,which will be given automatically from heroku.And you hit other routes like /home
I hope this help you,but check other posts to be sure.
I've been doing this myself, instead of starting with barebones up to skeleton and onwards just download this base web app which you can immediately deploy to Heroku.. Link is https://github.com/hubspot/basewebapp
from there, initiate a repo on github from that link and deploy on Heroku via deployment and with automatic deploys, change on VSC or GitHub, changes apply to the webpage/webapp on Heroku.
If you need any other help, feel free to contact me.. Been surfing Heroku apps for a few weeks already.
Check www.conid.dev for as far as I've published so far

Flutter web app fails to get node API on the same pc

I have created a flutter web app which uses NodeJS API to fetch data from mongodb database and write some data to mongodb. The app works fine when i run it as a Desktop app,it communicates to my node server. The app also work fine when i run it on chrome using
flutter run -d chrome
from android studio terminal. But when i build the web app and run it with the bellow command only the static page loads and it is not fetching data from mongoDB. It isnot communicating with NodeJS API.
flutter build web
cd build/web
python -m http.server 8000
So all i want was to deploy my flutter web app to a server and check if it works properly, but it fails to connect to the NodeJS back end. What is the best way to do it,Deploy a Flutter web app with also uses NodeJS APIs to fetch some JSON data.
Enable CORS for API. Here is an example which enables it for all requests.
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
...
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
See cors for details.

Trying to run node app on fedora server

Ok I am making website and want to use mongo, express, etc. I setup a server using fedora server ISO. The problem is getting node working. I have followed several tutorials, and its all the same. Nothing works. So I have to be doing something wrong. Trying to get the simplest thing to display on screen.
I think the server is running httpd server, whatever fedora has built in. I get the default fedora server page when going to the url. So the server is running and working, just hasn't been configured. When running node on the server do I have to use httpd-node? Or can it be http, etc.
Here is my app.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
And then I have a basic index.html that should be rendered just saying test.
I ssh into the server and run node start, it runs and the console logs the message like it should. But if I go to the address 192.168.1.5, or the domain that points to the server, I get nothing, just a blank page.
If someone can help me get this working, I can actually get to work coding out the application. Any help would be appreciated.
I think you make a confusion. When you build an Express application, you do not need another server at all.
When you start your app with:
app.listen(3000, function () {})
Express returns an http.Server object listening to port 3000.
When you navigate to your local adress on port 3000, you will see your "hello world" message.
It is possible that httpd service is already running in your Fedora environnement on default port 80 (the default port for http, the one your reach when you go to your local adress) but this is an Apache server and you do not need it to run your Nodejs app.
To build a Nodejs server, you can also use httpd-node package, but this is redundant as you're using Express framework.
If you need to serve a simple html file, a method I like for its simplicity is to use ejs template engine, something like this.
res.send('Hello World!') - this is your problem! Why?
How you receive this answer on client side?
Solution: use res.render(..) - for rendering from server or use AJAX on client side for receive this text!
P.S: render page and you don't see blank page anymore! Or use client-server conversation logic with your server through AJAX.
Try 192.168.1.5:3000
If I wrong: show your full project setup...
Test your app with curl (https://curl.haxx.se)! Check connection establishment, and show results here!

Azure web deployment not displaying web page

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.

Why won't simple node.js app doesn't respond on Windows Azure VM?

I am new to node.js, so hopefully I'm missing something obvious.
I have a Windows Azure VM running Windows Server 2012. It has IIS installed and simple, static sites returning static HTML works fine.
I have installed node.js on this server (via Chocolatey). I've created a simple Hello World node.js application (test.js):
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(80);
I fire this up on the server via: node test.js
It works fine on the server when I browse via http://localhost/test.js
It is unreachable from my client machine's browser via http://<servername>/test.js
I have:
created an endpoint on my VM to allow port 80 via TCP;
created a firewall rule on my VM to allow port 80 traffic
a web site on IIS for port 80 and it's running
When I change the above code to listen on a different port (e.g. 2368) and make the appropriate endpoint and firewall rules, everything works great both on the client and the server. I have no problem accessing the site.
What am I missing with port 80 here? Why can't I access my test file via port 80, but I can access it via a different port?
Hopefully it's something obvious. Thank you in advance.

Resources