Saturday, September 9, 2017

[google-cloud-sql-discuss] cloudsql connection and insert problem

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;

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/3b23a51d-39ee-47a5-bc68-f14c752a8799%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment