I have developed a REST api using node.js. My api is running on my macbook. I am trying to access MS SQL server running on another machine using node-mysql module, but while trying to create a connection I am getting the following error:
GET /contacts/ 200 12.467 ms - - events.js:141 throw er; // Unhandled 'error' event ^
Error: Cannot enqueue Handshake after invoking quit. at Protocol._validateEnqueue (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/mysql/lib/protocol/Protocol.js:202:16) at Protocol._enqueue (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/mysql/lib/protocol/Protocol.js:135:13) at Protocol.handshake (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/mysql/lib/protocol/Protocol.js:52:41) at Connection.connect (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/mysql/lib/Connection.js:123:18) at read_json_file (/Users/abc/Desktop/NodeProjects/MyWebsite/models/contacts.js:15:14) at Object.exports.list (/Users/abc/Desktop/NodeProjects/MyWebsite/models/contacts.js:29:22) at /Users/abc/Desktop/NodeProjects/MyWebsite/app.js:38:44 at Layer.handle [as handle_request] (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/express/lib/router/layer.js:95:5) at next (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/abc/Desktop/NodeProjects/MyWebsite/node_modules/express/lib/router/route.js:112:3).
I have no Clue what this is saying, Can some one guide me through this?
The code of my two scripts is given below for understanding the problem? I am trying to connect to the MS Sql server through contacts.js scripts's "function read_json_file()" function.
App.js script code:
var express = require('express');
var http = require('http');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var url = require('url');
var routes = require('./routes/index');
var contacts = require('./models/contacts');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('port', process.env.PORT || 3000);
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
//app.use('/contacts', contacts);
// catch 404 and forward to error handler
app.get('/contacts',function(request, response){
var get_params = url.parse(request.url, true).query;
if (Object.keys(get_params).length == 0)
{
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(contacts.list()));
}
else
{
response.setHeader('content-type', 'application/json');
stringify(contacts.query_by_arg(get_params.arg, get_params.value));
}
});
app.get('/contacts/:number', function(request, response) {
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(contacts.query(request.params.number)));
});
app.get('/groups', function(request, response) {
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(contacts.list_groups()));
});
app.get('/groups/:name', function(request, response) {
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(
contacts.get_members(request.params.name)));
});
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;
contacts.js script code:
var fs = require('fs');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.***.***', //Ip address of the server machine
port : '****', //Port number
user : 'ab',
password : 'abc',
database : 'MyDataBase'
});
//Read Json file
function read_json_file() {
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
var file = './data/contacts.json';
return fs.readFileSync(file);
}
//Parse the the file da
exports.list = function() {
return JSON.parse(read_json_file());
};
exports.query = function(number) {
console.log('contact Number is:: '+number);
var json_result = JSON.parse(read_json_file());
var result = json_result.result || [];
for (var i = 0; i < result.length; i++) {
var contact = result[i];
if (contact.primarycontactnumber === number) {
return contact;
}
}
return null;
};
exports.query_by_arg = function(arg, value) {
var json_result = JSON.parse(read_json_file());
var result = json_result.result || [];
for (var i = 0; i < result.length; i++) {
var contact = result[i];
if (contact[arg] === value) {
return contact;
}
}
return null;
};
exports.list_groups = function() {
var json_result = JSON.parse(read_json_file());
var result = json_result.result || [];
var resultArray = [];
for (var i = 0; i < result.length; i++) {
var groups = result[i].groups;
for (var index = 0; index < groups.length; index++) {
if (resultArray.indexOf(groups[index]) === -1) {
resultArray.push(groups[index]);
}
}
}
return resultArray;
};
exports.get_members = function(group_name) {
var json_result = JSON.parse(read_json_file());
var result = json_result.result || [];
var resultArray = [];
for (var i = 0; i < result.length; i++) {
if (result[i].groups.indexOf(group_name) > -1) {
resultArray.push(result[i]);
}
}
return resultArray;
};
Use mussel module https://github.com/patriksimek/node-mssql . Follow the samples.