I'm trying to understand how to use the node js util to promisify pool connections.
I would keep my code clean using async/await logic ( without the callback hell, that I really don't like especially with transactions ).
here is my config file:
const mysql = require('mysql');
const util = require('util');
const pool = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
// Ping database to check for common exception errors.
pool.getConnection((err, connection) => {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.error('Database connection was closed.')
}
if (err.code === 'ER_CON_COUNT_ERROR') {
console.error('Database has too many connections.')
}
if (err.code === 'ECONNREFUSED') {
console.error('Database connection was refused.')
}
}
if (connection) connection.release();
return;
})
// Promisify for Node.js async/await.
pool.query = util.promisify(pool.query)
module.exports = pool;
I'm executing single easy queries like this (and they work fine):
let sql = "SELECT * FROM products WHERE code = ? ;"
let param = [code];
let results = await pool.query(sql, param);
I'm developing transactions in this way ( I think it is a completely wrong approach ):
try {
await pool.query('START TRANSACTION');
sql = "INSERT INTO test (name) VALUES ( ? ) ;"
param = ['pippo'];
results = []
await pool.query(sql, param);
await pool.query('COMMIT');
} catch (error) {
await pool.query('ROLLBACK');
return next(error)
}
With transactions I shouldn't use pool.query ( that, I think, get each time a new connection and automatically release it when the query is finished ).
It seems to me that pool.query create a big problem with transactions:
If just run one transaction at a time is ok, but if run 2 (or more) transactions at the same time maybe the COMMIT of the second transaction can COMMIT all the queries of the first transaction just because is executed before the first COMMIT.
I think I should instead get a new connection use the connection for the entire flow of the transaction and release it at the end. So each transaction flow needs an own connection.
But I don't know how to promisify a pool.getConnection as I promisify the pool.query.
I was trying something like:
pool.getConnection = util.promisify(pool.getConnection).bind(pool)
const conn = await pool.getConnection();
let sql = "SELECT * FROM test ;"
let param = [];
let results = await conn.query(sql); // I don't get here the expected rows
but it doesn't work. I don't become the rows in result but if i console.log(results) I have this:
Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (C:\Users\rocco\wa\ferramenta\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at PoolConnection.query (C:\Users\rocco\wa\ferramenta\server\node_modules\mysql\lib\Connection.js:198:25)
at C:\Users\rocco\wa\ferramenta\server\routes\imports.js:304:30
at processTicksAndRejections (internal/process/task_queues.js:97:5),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'SELECT * FROM test ; ',
values: [],
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: PoolConnection {
_events: [Object: null prototype] {
end: [Function: _removeFromPool],
error: [Function]
},
_eventsCount: 2,
_maxListeners: undefined,
config: ConnectionConfig {
host: 'localhost',
port: '3306',
localAddress: undefined,
socketPath: undefined,
user: 'root',
password: '---myPassword---',
database: '---nameOfMyDb---',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: [Pool],
ssl: false,
localInfile: true,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631,
protocol41: true
},
_socket: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: 'localhost',
_readableState: [ReadableState],
readable: true,
_events: [Object: null prototype],
_eventsCount: 4,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
timeout: 0,
[Symbol(asyncId)]: 11,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: Timeout {
_idleTimeout: -1,
_idlePrev: null,
_idleNext: null,
_idleStart: 1284,
_onTimeout: null,
_timerArgs: undefined,
_repeat: null,
_destroyed: true,
[Symbol(refed)]: false,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 14,
[Symbol(triggerId)]: 1
},
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
},
_protocol: Protocol {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [ConnectionConfig],
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshake: true,
_handshaked: true,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: [HandshakeInitializationPacket],
_parser: [Parser],
[Symbol(kCapture)]: false
},
_connectCalled: true,
state: 'authenticated',
threadId: 117,
_pool: Pool {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: [PoolConfig],
_acquiringConnections: [],
_allConnections: [Array],
_freeConnections: [],
_connectionQueue: [],
_closed: false,
query: [Function],
getConnection: [Function: bound ],
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
}
Some ideas ?
THX
Here is an easy example about how I solved it:
pool.getConnection = util.promisify(pool.getConnection)
let conn = await pool.getConnection();
conn.query = util.promisify(conn.query)
sql = "INSERT INTO test (name) VALUES ( ? ) ; ";
param = ['fakename'];
results = [];
results = await conn.query(sql, param)
conn.release(); // is important remember to release the connection
Related
Currently I want to access MySQL Aurora DB from inside a lambda function.
I have both the Aurora and the lambda in same VPC, subnets and security group.
The following is the lambda function code:
console.log("Welcome to lambda")
var connection = mysql.createConnection({
host: 'aurora-test1-us-mysql-multilevel.cluster-cutke7yxox3c.us-west-2.rds.amazonaws.com',
database: 'aurora-test1-us-mysql-multilevel',
user: "***",
password: "***",
});
console.log("createConnection",connection)
let con = connection.connect();
console.log("connect:",con)
const query = connection.query('USE feature;SELECT * FROM feature;')
console.log("query",query)
connection.end();
return {
statusCode: 200,
body: "HELLO KIMOO!!! Welcome TO AURORA DB" + "Database Created2"
}
}
catch(err)
{
console.log("Error caught",err)
return {
statusCode: 500,
body: JSON.stringify({
message: 'Error: ' + err
})
}
}
The following is the output received from logging the query
_events: [Object: null prototype] {
error: [Function (anonymous)],
packet: [Function (anonymous)],
timeout: [Function (anonymous)],
end: [Function (anonymous)]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)
at Runtime.<anonymous> (/var/task/index.js:39:38)
at Generator.next (<anonymous>)
at /var/task/index.js:8:71
at new Promise (<anonymous>)
at __awaiter (/var/task/index.js:4:12)
at Runtime.handler (/var/task/index.js:17:12)
at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1089:29),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular *1], _timeout: null },
sql: 'USE feature;SELECT * FROM feature;',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: <ref *2> Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: ConnectionConfig {
host: 'aurora-test1-us-mysql-multilevel.cluster-cutke7yxox3c.us-west-2.rds.amazonaws.com',
port: 3306, ......
I don't understand what does this error mean and how can I connect to Aurora and retrieve data from it successfully
Note:
I'm using Typescript
I'm using mysql library
I am working in express and Node-js and I am working in MySql in Node. I want to store the user data in the database but checking whether the user is already present or not. the working is that on the starting of the function it call the userpresent function to check that the is present or not. I want to return the true or false but I is returning an object and I cannot handle it the code is what I do in this code to use the return value(which must be true or false) in route function:
const express = require('express');
const router = express.Router();
const database = require('../DBconfig');
var mysql = require('mysql');
const { body, validationResult } = require('express-validator');
// to make connection with the database
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'yourguide',
});
// connection statement is end
//the route function is start
// here I added some express validators
router.post(
'/signup',
[
body('username', 'enter username of min 4 character').isLength({ min: 3 }),
body('firstName', 'enter firstName of min 3 character').isLength({
min: 3,
}),
body('lastName', 'enter lastname of min 3 character').isLength({ min: 3 }),
body('password', 'enter password of min 4 character').isLength({ min: 4 }),
body('city', 'enter city of min 4 character').isLength({ min: 4 }),
body('email', 'entera valid email').isEmail(),
],
async (req, res) => {
const error = validationResult(req);
//check that the user enter a valid input
var flag = await userPresent(req.body.username, req.body.email);
if (!error.isEmpty()) {
return res.status(400).json({ error: error.array() });
} else {
// if the input is valid then start to check the user
await connection.connect(function (err) {
if (err) console.log('not conntected to table');
else console.log('connected to table');
// call the async function to check the presence of the user
console.log('flag in main is ');
console.log(flag);
//check the return value of the fucntion
//if the user is not present then execute the if statement
if (!flag) {
//make the query
var query = `INSERT INTO users(firstName, lastName, username, email, city, password) VALUES ("${req.body.firstName}","${req.body.lastName}","${req.body.username}","${req.body.email}","${req.body.city}","${req.body.password}")`;
console.log(query);
connection.query(query, function (err, result) {
if (err) {
flag = false;
console.log('not inserted');
return flag;
} else {
flag = true;
console.log('inserted');
return flag;
}
});
return res.body;
} else {
return res
.status(400)
.json({ error: 'given email or username is already exist' });
}
});
}
}
);
//async function to check the user presence
userPresent = async (username, email) => {
//initialize the flag with true
let data = '';
//make query to check the user
var query = `SELECT * FROM users WHERE username= "${username}" AND email="${email}" `;
data = await connection.query(
query,
(present = (err, result) => {
return result != null;
})
);
console.log('data is ', data);
};
the userpresent function return the object
_events: [Object: null prototype] {
error: [Function (anonymous)],
packet: [Function (anonymous)],
timeout: [Function (anonymous)],
end: [Function (anonymous)]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: [Function (anonymous)],
_callSite: Error
at Protocol._enqueue (D:\practise\yourguide\yourguide\backend\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (D:\practise\yourguide\yourguide\backend\node_modules\mysql\lib\Connection.js:198:25)
at userPresent (D:\practise\yourguide\yourguide\backend\routes\user.js:83:29)
at D:\practise\yourguide\yourguide\backend\routes\user.js:28:22
at Layer.handle [as handle_request] (D:\practise\yourguide\yourguide\node_modules\express\lib\router\layer.js:95:5)
at next (D:\practise\yourguide\yourguide\node_modules\express\lib\router\route.js:137:13)
at middleware (D:\practise\yourguide\yourguide\backend\node_modules\express-validator\src\middlewares\check.js:16:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular *1], _timeout: null },
sql: 'SELECT * FROM users WHERE username= "harry13" AND email="harry13#gmail.com" ',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: <ref *2> Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: ConnectionConfig {
host: 'localhost',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: 'root',
password: 'root',
database: 'yourguide',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
ssl: false,
localInfile: true,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631
},
_socket: Socket {
connecting: true,
_hadError: false,
_parent: null,
_host: 'localhost',
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
timeout: 10000,
[Symbol(async_id_symbol)]: 36,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: Timeout {
_idleTimeout: 10000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 3579,
_onTimeout: [Function: bound ],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: false,
[Symbol(kHasPrimitive)]: false,
[Symbol(asyncId)]: 39,
[Symbol(triggerId)]: 0
},
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
},
_protocol: Protocol {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [ConnectionConfig],
_connection: [Circular *2],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshake: true,
_handshaked: false,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: null,
_parser: [Parser],
[Symbol(kCapture)]: false
},
_connectCalled: true,
state: 'disconnected',
threadId: null,
[Symbol(kCapture)]: false
},
[Symbol(kCapture)]: false
}
what I do to return the true or false to use in our route function
I'm new in node.js. I using AWS with node.js and MySQL to create a lambda function, but I can't catch the results in my handler, my database instance it's in RDS service.
When I launch my callback in the console it shows me a giant and unreadable json with information from the database but not from the records that I am consulting, why is this? because it happens, am I doing something wrong?
This is my code:
'use strict';
const AWS = require("aws-sdk");
AWS.config.update( { region: "us-west-2" } );
var mysql = require('mysql');
var querystring = require('querystring');
const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
database : process.env.RDS_DB_NAME
});
exports.handler = async (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const results = await con.query('SELECT * FROM `documentoApp_rptenviadoclon`;');
await con.end();
if (results) {
callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Requested-With',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(results)
});
} else {
callback('error', {
statusCode: 400,
headers: {
'Access-Control-Allow-Headers': 'Content-Type,X-Amz-Date,Authorization,X-Requested-With',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*'
},
body: {
message: 'No results found.'
},
});
}
};
I tried to put a console.log in response body and this is what it shows, the giant and illegible json of which I speak:
START RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035 Version: $LATEST
2020-03-20T21:57:15.134Z 80dfacdb-034b-41bc-8c53-e5eb3347d035 INFO Query {
_events: [Object: null prototype] {
error: [Function],
packet: [Function],
timeout: [Function],
end: [Function]
},
_eventsCount: 4,
_maxListeners: undefined,
_callback: undefined,
_callSite: Error
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:198:25)
at Runtime.exports.handler (/var/task/index.js:19:31)
at Runtime.handleOnce (/var/runtime/Runtime.js:66:25),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'SELECT * FROM `documentoApp_rptenviadoclon`;',
values: undefined,
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection: Connection {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: ConnectionConfig {
host: '--hidden--',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: '--hidden--',
password: '--hidden--',
database: '--hidden--',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
ssl: false,
localInfile: true,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631
},
_socket: Socket {
connecting: true,
_hadError: false,
_parent: null,
_host: 'dbportalreportes-dev.cjawt1xkypqu.us-west-2.rds.amazonaws.com',
_readableState: [ReadableState],
readable: false,
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
timeout: 10000,
[Symbol(asyncId)]: 3,
[Symbol(kHandle)]: [TCP],
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: Timeout {
_idleTimeout: 10000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 469,
_onTimeout: [Function: bound ],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(refed)]: false,
[Symbol(asyncId)]: 7,
[Symbol(triggerId)]: 0
},
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0
},
_protocol: Protocol {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [ConnectionConfig],
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: [Quit],
_handshake: true,
_handshaked: false,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: null,
_parser: [Parser]
},
_connectCalled: true,
state: 'disconnected',
threadId: null
}
}
END RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035
REPORT RequestId: 80dfacdb-034b-41bc-8c53-e5eb3347d035 Duration: 208.98 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 92 MB Init Duration: 415.02 ms
I hope you can help me, thank you very much!
Here is my JSON response. I want to parse the below JSON and get Site_id from header.location. I am able to get Status, Message values but not location value.
It is actually response header, I am trying to parse.
Please, anyone can help here?
var apigClientFactory = require('aws-api-gateway-client').default;
let awsbody = JSON.parse(process.argv[2].split('\\').join('') || '{}');
var apigClient = apigClientFactory.newClient({
invokeUrl: awsbody.endPoint, // REQUIRED
accessKey: awsbody.awsAccessKey, // REQUIRED
secretKey: awsbody.awsSecreteKey, // REQUIRED
region: awsbody.awsRegion, // REQUIRED: The region where the AapiKeyloyed.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried. Uses axon-
retry plugin.
return err.response && err.response.status === 500;
}
});
var pathParams = awsbody.awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsbody.awsPathTemplate; // '/api/v1/sites'
var method = awsbody.method; // 'POST';
var additionalParams = awsbody.awsAdditionalParams; //queryParams & Headers if any
//var additionalParams = {headers: { "x-apigw-api-id": 'vs2i50xvo4'}};
var body = awsbody.requestBody;
var output = {};
apigClient.invokeApi(pathParams, pathTemplate, method, additionalParams, body)
.then(function(result) {
console.log(result)
}).catch(function(result) {
console.log(result)
});
Here is output: But I just need Status, StatusText, location under headers and data (# bottom) as output. I am tried to parse the output in above code no luck.
Can anyone help to parse response.
{ status: 202,
statusText: 'Accepted',
headers:
{ server: 'Server',
date: 'Tue, 12 Mar 2019 20:34:36 GMT',
'content-type': 'application/json',
'content-length': '61',
'x-amzn-requestid': '400377cd-4506-11e9-b941-3984677a719c',
'x-amz-apigw-id': 'WciqVH8pPHcFgWg=',
location:
'/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974
'x-amzn-trace-id': 'Root=1-5c8817db-5522b10bf2068ac2509ae35b;Samp
connection: 'close' },
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json',
'Content-Type': 'application/json',
'x-apigw-api-id': 'vs2i50xvo4',
'x-amz-date': '20190312T203427Z',
Authorization:
'AWS4-HMAC-SHA256 Credential=AKIAJ4Y5DGRDNBQ2HBYA/20190312/us
cept;content-type;host;x-amz-date;x-apigw-api-id, Signature=801d0887fd
a249',
'User-Agent': 'axios/0.18.0',
'Content-Length': 245 },
method: 'post',
data:
'{"site_id":"TDCLOUDTSTTD03","account_id":"109443356002","change
ider":"aws","region":"us-west-2","network_id":"vpc-0ab26280d5060105c",
01.128/25"]}',
url:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.v
ites' },
request:
ClientRequest {
_events:
[Object: null prototype] {
socket: [Function],
abort: [Function],
aborted: [Function],
error: [Function],
timeout: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 6,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
_readableState: [ReadableState],
readable: true,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 6,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 8,
connecting: false,
_hadError: false,
_handle: [TLSWrap],
_parent: null,
_host:
'vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.vpce.a
_readableState: [ReadableState],
readable: true,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [TLSWrap],
_requestCert: true,
_rejectUnauthorized: true,
parser: null,
_httpMessage: [Circular],
[Symbol(res)]: [TLSWrap],
[Symbol(asyncId)]: 6,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(connect-options)]: [Object] },
_header:
'POST /qa/transit-connectivity/api/v1/sites HTTP/1.1\r\nAccept:
\r\nx-apigw-api-id: vs2i50xvo4\r\nx-amz-date: 20190312T203427Z\r\nAuth
DNBQ2HBYA/20190312/us-west-2/execute-api/aws4_request, SignedHeaders=a
Signature=801d0887fd4a8b86a1af51a02cede529962fc5cae84f4a10f23983100e0
h: 245\r\nHost: vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-2.
_onPendingData: [Function: noopPendingOutput],
agent:
Agent {
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'POST',
path: '/qa/transit-connectivity/api/v1/sites',
_ended: true,
res:
IncomingMessage {
_readableState: [ReadableState],
readable: false,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
socket: [TLSSocket],
connection: [TLSSocket],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Array],
trailers: {},
rawTrailers: [],
aborted: false,
upgrade: false,
url: '',
method: null,
statusCode: 202,
statusMessage: 'Accepted',
client: [TLSSocket],
_consuming: true,
_dumped: false,
req: [Circular],
responseUrl:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites',
redirects: [] },
aborted: undefined,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable:
Writable {
_writableState: [WritableState],
writable: true,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
_options: [Object],
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 245,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: [Circular],
_currentUrl:
'https://vpce-09d3e2325f36b5527-8xogk1r2.execute-api.us-west-
1/sites' },
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]:
[Object: null prototype] {
accept: [Array],
'content-type': [Array],
'x-apigw-api-id': [Array],
'x-amz-date': [Array],
authorization: [Array],
'user-agent': [Array],
'content-length': [Array],
host: [Array] } },
data: { status: 'success', message: 'request has been accepted' } }
varible result is not in json format actually, take a look at my code which is in valid Json format.
You can get location as you need now.
let v = '{"data": {"status": "sucess","message": "request has been accepted"},"responsecode": 202,"responsetext": "Accepted","headers": {"server": "Server","date": "Tue, 12 Mar 2019 20:34:36 GMT","content-type": "application/json","content-length": "61","x-amzn-requestid": "400377cd-4506-11e9-b941-3984677a719c","x-amz-apigw-id": "WciqVH8pPHcFgWg=","location": "/transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974","x-amzn-trace-id": "Root=1-5c8817db-5522b10bf2068ac2509ae35b;Sampled=0","connection": "close"}}';
let value = JSON.parse(v);
console.log("location :"value.headers.location);
Console:
location : /transit-connectivity/api/v1/jobs/8125d985-3d90-473d-b799-c9974
Here the code which I am using for auto notifying if there is any change in mySql database to nodeJS, it dosn't notifying?, at the same time I have edited my.cnf file also
#my.cnf file
binlog_format = row
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = test # Optional, limit which databases to log
Node JS
var MySQLEvents = require('mysql-events');
var dsn = {
host: 'localhost',
user: 'root',
password: 'root',
};
var mysqlEventWatcher = MySQLEvents(dsn);
//console.log(mysqlEventWatcher);
var watcher = mysqlEventWatcher.add(
'test.eventTable',
// test - database name, eventTable - table name
function (oldRow, newRow) {
//row inserted
console.log(oldRow);
console.log('************');
console.log(newRow);
console.log('************');
if (oldRow === null) {
//insert code goes here
}
//row deleted
if (newRow === null) {
//delete code goes here
}
//row updated
if (oldRow !== null && newRow !== null) {
//update code goes here
}
},
'Active'
);
and i checked if my connection is established or not from node side like console.log(mysqlEventWatcher);
it responds like
{ started: false,
zongji:
ZongJi {
options: {},
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
ctrlConnection:
Connection {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
config: [Object],
_socket: [Object],
_protocol: [Object],
_connectCalled: true,
state: 'disconnected',
threadId: null },
ctrlCallbacks: [],
connection:
Connection {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
config: [Object],
_socket: undefined,
_protocol: [Object],
_connectCalled: false,
state: 'disconnected',
threadId: null },
tableMap: {},
ready: false,
useChecksum: false },
databases: [],
tables: {},
columns: {},
events: [ 'tablemap', 'writerows', 'updaterows', 'deleterows' ],
triggers: [],
connect: [Function],
add: [Function],
remove: [Function],
stop: [Function],
reload: [Function],
includeSchema: [Function] }
two things you should consider modifying :
-for configuration try this :
server_id=1 (this is the only important conf you are missing in your file)
log-bin = binlog
binlog_format = row expire-logs-days = 14 (not important to make
things working) max-binlog-size = 500M (not important to make
things working)
-remove 'Active' from mysqlEventWatcher.add