I currently have Google cloud Compute engine running nodejs that is connected to a Google Cloud SQL witch takes data from MySQL workbench. This is my server.js file that I run with 'node'
var gcloud = require("gcloud");
var express = require("express");
var http = require('http');
var mysql = require("mysql");
var connection = mysql.createConnection({
port: "3306",
host : "173.194.87.72",
user : "root",
password : "****",
database : "scripto_5_k"
});
var app = express();
/* Connection to Database */
connection.connect(function(error){
if(error)
{
console.log("Problem with MySQL "+error);
}
else
{
console.log("Connected with Database ");
}
});
app.set('port', process.env.PORT || 3000);
app.get('/',function(req,res){
res.send('hello index');
});
app.get('/load',function(req,res){
console.log("/load hit");
connection.query("SELECT * from user",function(err,rows){
if(err)
{
console.log("Problem with MySQL"+err);
}
else
{
res.end(JSON.stringify(rows));
}
});
});
/*start the server */
app.listen(3000,function(){
console.log("its started on PORT %s", app.settings.port);
});
this should take all the data in the user table and put it to JSON when I use /load. But I cant get Postman to display the data. This is the address I have been using
http://130.211.90.249:3000/load
The address is from Google cloud the port is the one I set in the server.js file and /load should use the function I have written above.
Why am I not seeing a result with this input? I am running the server with 'node server.js' on my VM instance and it says I have successfully connected.
Any help is really appreciated.
Edit 1
This is what my VM looks like when I run the server.js
my_user_name@nodejs-2:~$ node server.js
its started on PORT 3000
Connected with Database
No error but in Postman I get "Could not get any response" when I put in the address given above.
Edit 2
Do I need to create a http server in the server.js file that I run so that the JSON object can be reached by the request in Postman?
Edit 3
If I follow the ip address of my google cloud VM it takes me to a default page for Bitnami but tells me that node js is running in the cloud. Do I need to remove this default page or is it fine to just leave whilst the server runs?
Here is a link http://130.211.90.249/
Since the app is listening on port 3000 in your code, you need to create a firewall rule to make that port available from the outside.
From the developer console for that project, go to Networks and view the details for the default network. Create a rule to allow traffic on port 3000.