cloudsql connection and insert problem

50 views
Skip to first unread message

Xiaoyi Wang

unread,
Sep 9, 2017, 1:31:42 PM9/9/17
to Google Cloud SQL discuss
dear all,

I wrote a simple cloudsql connection and insert code, but it does not work. I use node.js as the server part.
I run the code on localhost but connect to the google cloudsql.

The problem is the server makes a new connection but after a few seconds, the instance closed the connection. the server keeps making new connections after it is closed by instance. Data cannot be inserted into the database. I pasted the code below. Any ideas what is wrong with my code or my configuration? Thank you very much.
'use strict';
const express = require('express')
const app = express()
var path = require('path');
var SqlString = require('sqlstring');
var bodyParser = require('body-parser')
var mysql = require('mysql');
// Require process, so we can mock environment variables
const process = require('process');
// [START app]
// [START setup]
const Knex = require('knex');
const crypto = require('crypto');
app.enable('trust proxy');
let knex;

app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));



app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'view')));

app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});

app.post('/save_question/', function(req, res) {


if(req.body)
{
var currentQ = parseInt(req.body.currentQ);
if(currentQ == 0 || currentQ == 1)
{
//insert
}
else if(currentQ > 1 && currentQ < 12)
{
var task = buildInsertSql(req);
var returning = insertTask(task);
}
}

});


// Get type of SQL client to use
const sqlClient = process.env.SQL_CLIENT;
//console.log("process.env: ", process.env);
if (sqlClient === 'pg' || sqlClient === 'mysql')
{
console.log("connection!!!!!!!!!!!!!");
knex = connect();
}
else
{
throw new Error(`The SQL_CLIENT environment variable must be set to lowercase 'pg' or 'mysql'.`);
}

function buildInsertSql(req)
{
var data = req.body;
var obj = {};
////////////////logic
return obj;
}

// [START listen]
const PORT = process.env.PORT || 8080;
const server = app.listen(PORT, () => {
const port = server.address().port;
console.log(`App listening on port ${PORT}`);
});

function connect () {
// [START connect]
var config = {
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
};

if (process.env.INSTANCE_CONNECTION_NAME)
{
if (process.env.SQL_CLIENT === 'mysql')
{
console.log("sql......");
config.socketPath = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`;
}
else if (process.env.SQL_CLIENT === 'pg')
{
config.host = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`;
}
}
Knex({
client: process.env.SQL_CLIENT,
connection: config
})

}

function insertTask(task)
{
return knex('surveytasks')
.insert(task);
}

module.exports = app;

Kamran (Google Cloud Support)

unread,
Sep 9, 2017, 8:02:08 PM9/9/17
to Google Cloud SQL discuss
This seems like a question that would be better posted on stackoverflow.com, rather than being a discussion post. This isn't the right place to look for 1-on-1 support, though, being a forum meant for general discussion of the platform. Posting on stackoverflow, being sure to include all relevant details and error messages which would help somebody debug.

For more information about Community Support please visit this article.

Sincerely,
Reply all
Reply to author
Forward
0 new messages