How to query mysql before starting http server in nodejs - mysql

A simple question, I use nodejs for HTTP restful API service. Now I want query some mysql data before http server start, the code snippet:
var price = -1;
var userId = 'some user provided value';
var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
price = results.price;
});
server = http.createServer(function (request, response) {
handler(request); //response depend on price
}).listen(8012);
So how to combine mysql query and http server code?

Although this is a simple question the size of the answer is huge! Tim Ruffles gave a whole lecture on the subject at the London Node Users Group this week (July 2013) which is available as a video lecture [http://www.youtube.com/watch?v=nHXKA82M-LY].
The problem here is the fundamental one of how to handle something that needs to be done synchronously (in sequence) while working in an asynchronous (no order guaranteed) programming environment.
Of the list of ways Tim uses to deal with this problem there are three which seem to be able to help you here.
As suggested in other posts, you could use a callback from your code:
var price = -1;
var userId = 'some user provided value';
var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
price = results.price;
//start the server afer the sql connection has called back
server = http.createServer(function (request, response) {
handler(request); //response depend on price
}).listen(8012);
});
However this leads to what is called 'pyramid code' and can become unmanageable quite quickly.
My personal preference to solve this problem would be to use the async library [https://github.com/caolan/async]which contains an arsenal of tools for dealing with sequencing problems in node. I choose this because it seems the most intuitive way to me.
var async = require('async');
functon query(callback){
var price = -1;
var userId = 'some user provided value';
var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
price = results.price;
callback();
});
}
function boot(callback){
server = http.createServer(function (request, response) {
handler(request); //response depend on price
callback();
}).listen(8012);
}
async.series([query,boot]);
You can install async with npm install async.
Or the third approach that might reasonably be used for this problems is to use promises. These are well accepted, by while learning node coming from a language other javascript, I found this quite hard to adapt to coming from a language other then javascript, but programmers who use JQuery seem to favour this approach. There is a tutorial of how use promises in node.js here : http://howtonode.org/promises
Tim of course provides a much more in-depth analysis and comparison in his lecture of these approaches and supplies a few insights of how we might do this in the future.
You might also find the modules Express, Restify and Restler useful as a way to create a Restful api.
*disclaimer I haven't run this code as I don't have mysql installed - opportunity for an edit here :)

You just add the server inside of the return callback for the connection query, like so:
var price = -1;
var userId = 'some user provided value';
var sql = 'SELECT max(price) FROM users WHERE id = ' + connection.escape(userId);
connection.query(sql, function(err, results) {
price = results.price;
server = http.createServer(function (request, response) {
console.log(price);
handler(request, price, response); //response depend on price
}).listen(8012);
});

Related

problem with using mysql with nodejs server with transaction query

So essentially I am using a transaction in my nodejs server with mysql to pull the first row and then delete that row from the table.
the code for this looks like:
app.get('/', (req, res) => {
connection.query("START TRANSACTION; SELECT * FROM data.train_data LIMIT 1; DELETE FROM data.train_data LIMIT 1; COMMIT;",(err,rows) => {
if(err){
console.log('Error selecting tweets from Database...');
return;
}
console.log('Successfully recieved tweets from database');
res.render('index', {
title: 'Tweet Labeler',
data: rows[1]
});
});
The problem is that even though the query is working correctly, when I reroute to the page in my client side javascript like this:
function relClick(){
var xhr = new XMLHttpRequest();
var newData = {'tweet':data[0].tweet,'relevant':1};
var jsonData = JSON.stringify(newData);
xhr.open("POST","/",true);
xhr.setRequestHeader('Content-type','application/json');
xhr.send(jsonData);
console.log("Clicked Relevant");
xhr = new XMLHttpRequest();
xhr.open("GET","/",true);
xhr.send();
nextItem();
}
I essentially want to repeat this process and grab the new top row from the table but by using print statements I have deduced that it is somehow not realizing that the row has been deleted even though when I use mysql workbench I can see the row has been deleted.
As you can see with this screenshot it continues to grab the row with the id 24 even though it has been deleted?
I am really confused on why this is. Hopefully someone with more experience can assist me because I am very new to this. Thanks in advance!

Do i need to connect to database every HTTP request in NodeJS?

I am using LocomotiveJS(MVC) based on ExpressJS for developing my first simple API.. I am still in learning phase.. I am using mysql as my database..
My question is, do i need to initiate a connection to mysql everytime there's a controller request?
Here's my code :
SongsController.show = function() {
//this.title = 'Locomotive';
console.log("nice imbasss");
var contacts = SongsModel.foo("GOOD");
var dbConnection = DBUtilities.connectMysql();
var contactsArr = [];
dbConnection.query('select * from contacts', function(err, rows, fields) {
//console.log(err);
console.log(rows);
//console.log(fields);
//contactsArr = rows;
});
DBUtilities.endMysql(dbConnection);
};
As you can notice, everytime songs/show is called, connectMysql() is called.. Am i doing right?
You should connect MySQL everytime when you fire a query to MySQL and should close your connection after that because mysql injection may occur by third party.

Trying to interpret the Node-Neo4j API

I'm pretty new to coding so forgive me if my code is unreadable or my question simplistic.
I am trying to create a little server application that (amongst other things) displays the properties of a neo4j node. I am using node.js, Express and Aseem Kishore's Node-Neo4j REST API client, the documentation for which can be found here.
My question stems from my inability to fetch the properties of nodes and paths. I can return a node or path, but they seem to be full of objects with which I cannot interact. I poured through the API documents looking for some examples of how particular methods are called but I found nothing.
Ive been trying to call the #toJSON method like, "db.toJSON(neoNode);" but it tells me that db does not contain that method. I've also tried, "var x = neoNode.data" but it returns undefined.
Could someone please help me figure this out?
//This file accepts POST data to the "queryanode" module
//and sends it to "talkToNeo" which queries the neo4j database.
//The results are sent to "resultants" where they are posted to
//a Jade view. Unfortuantly, the data comes out looking like
// [object Object] or a huge long string, or simply undefined.
var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://localhost:7474');
function resultants(neoNode, res){
// if I console.log(neoNode) here, I now get the 4 digit integer
// that Neo4j uses as handles for nodes.
console.log("second call of neoNode" + neoNode);
var alpha = neoNode.data; //this just doesn't work
console.log("alpha is: " +alpha); //returns undefined
var beta = JSON.stringify(alpha);
console.log("logging the node: ");
console.log(beta);// still undefined
res.render("results",{path: beta});
res.end('end');
}
function talkToNeo (reqnode, res) {
var params = {
};
var query = [
'MATCH (a {xml_id:"'+ reqnode +'"})',
'RETURN (a)'
].join('\n');
console.log(query);
db.query(query, params, function (err, results) {
if (err) throw err;
var neoNode = results.map(function (result){
return result['a']; //this returns a long string, looks like an array,
//but the values cannot be fetched out
});
console.log("this is the value of neoNode");
console.log(neoNode);
resultants(neoNode, res);
});
};
exports.queryanode = function (req, res) {
console.log('queryanode called');
if (req.method =='POST'){
var reqnode = req.body.node; //this works as it should, the neo4j query passes in
talkToNeo(reqnode, res) //the right value.
}
}
EDIT
Hey, I just wanted to answer my own question for anybody googling node, neo4j, data, or "How do I get neo4j properties?"
The gigantic object from neo4j, that when you stringified it you got all the "http://localhost:7474/db/data/node/7056/whatever" urls everywhere, that's JSON. You can query it with its own notation. You can set a variable to the value of a property like this:
var alpha = unfilteredResult[0]["nodes(p)"][i]._data.data;
Dealing with this JSON can be difficult. If you're anything like me, the object is way more complex than any internet example can prepare you for. You can see the structure by putting it through a JSON Viewer, but the important thing is that sometimes there's an extra, unnamed top layer to the object. That's why we call the zeroth layer with square bracket notation as such: unfilteredResult[0] The rest of the line mixes square and dot notation but it works. This is the final code for a function that calculates the shortest path between two nodes and loops through it. The final variables are passed into a Jade view.
function talkToNeo (nodeone, nodetwo, res) {
var params = {
};
var query = [
'MATCH (a {xml_id:"'+ nodeone +'"}),(b {xml_id:"' + nodetwo + '"}),',
'p = shortestPath((a)-[*..15]-(b))',
'RETURN nodes(p), p'
].join('\n');
console.log("logging the query" +query);
db.query(query, params, function (err, results) {
if (err) throw err;
var unfilteredResult = results;
var neoPath = "Here are all the nodes that make up this path: ";
for( i=0; i<unfilteredResult[0]["nodes(p)"].length; i++) {
neoPath += JSON.stringify(unfilteredResult[0]['nodes(p)'][i]._data.data);
}
var pathLength = unfilteredResult[0].p._length;
console.log("final result" + (neoPath));
res.render("results",{path: neoPath, pathLength: pathLength});
res.end('end');
});
};
I would recommend that you look at the sample application, which we updated for Neo4j 2.0
Which uses Cypher to load the data and Node-labels to model the Javascript types.
You can find it here: https://github.com/neo4j-contrib/node-neo4j-template
Please ask more questions after looking at this.

multiple websockets connections on the same client - server cannot handle

Let me clarify, this is kind of complicated.
I'm implementing a form to insert data in the database.
I have two websockets connections in the same client side, connecting on the same nodejs server.
One connection is triggered after the user inserts a name on the "name" textfield of the form. Sends the data to the server, server checks the database if the name already exists and responces back "This already exists. Mayde you are inserting something that is already there".
The other connection is triggered if all the fields of the form are not blank and sends the data to server to insert them in the database.
I thought it was a good idea to distinguish on server-side ,the different connections using arrays. If the first element of the array is "name" call the checkName function, or if it is "insert" , call the insertInDB function.
I created two small testing files. They do not work. Connections are open and the client sends the data. I get no errors inte server nor the client side. But server never responces. I dont get the expected numbers, back in the client side. I dont think this is the right anyway. This is complecated, I hope the code helps you.
Is it possible, what I am trying to do? Any hints or alternatives?
Thanks
the code....
server-side
function WebSocketTest1(){
var a=1;
var b=2;
var c = [a,b];
var so = new WebSocket("ws://localhost:1337");
so.onerror=function (evt)
{message.textContent = evt;}
so.onopen = function(){
message.textContent = "opened";
so.send(c);
message.textContent = "sended";
}
so.onmessage = function (evt) {
var received_msg = evt.data;
document.getElementById("message").innerHTML=received_msg;
}
}
function WebSocketTest2(){
var d=3;
var e=4;
var f = [d,e];
var sa = new WebSocket("ws://localhost:1337");
sa.onerror=function (evt)
{message2.textContent = evt;}
sa.onopen = function(){
message2.textContent = "opened";
sa.send(f);
message2.textContent = "sended";
}
sa.onmessage = function (evt) {
var received_msg = evt.data;
document.getElementById("message2").innerHTML=received_msg;
}
}
</script>
</head>
<input type="button" value="one" onClick="WebSocketTest1()"><br/>
<input type="button" value="two" onClick="WebSocketTest2()"><br/>
<body>
<div id="message"></div>
mesage2</br>
<div id="message2"></div>
</body>
</html>
on the server side I am listing the sessions, to communicate only with a specific session, code found here
and the server side (snippets)
var connections = {};
var connectionIDCounter = 0;
var connection = request.accept(null, request.origin);
// Store a reference to the connection using an incrementing ID
connection.id = connectionIDCounter ++;
connections[connection.id] = connection;
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
var ja=message;
if(ja[0]==1)
{ja[1]=7;}
else if(ja[0]==3)
{ja[1]=8;}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
delete connections[connection.id];
});
});
// Send a message to a connection by its connectionID
function sendToConnectionId(connectionID, data) {
var connection = connections[connectionID];
if (connection && connection.connected) {
connection.send(ja[1]);
}
I wonder why you need 2 connections.
Open one connection and send the payload as JSON.
e.g.
var ws = new WebSocket("ws://localhost:1336");
ws.send(JSON.stringify({ command: "checkname", params: "xxxx"; });
ws.send(JSON.stringify({ command: "submit", params: { ... });
At the server side, you just have to parse the payload and determine which command is executed.
I am just wondering why you need websocket. It seems that you need to validate user presence in the application. If user is not present then you need to insert in the database else throw error.
you can go through jquery post and jquery form validation
http://api.jquery.com/jQuery.post/

How to uniquely identify a socket with Node.js

TLDR; How to identify sockets in event based programming model.
I am just starting up with node.js , in the past i have done most of my coding
part in C++ and PHP sockets() so node.js is something extremely new to me.
In c++ to identify a socket we could have done something like writing a main socket say server to listen for new connections and changes, and then handling those connections accordingly.
If you are looking for actual sockets and not socket.io, they do exist.
But as stated, Node.js and Javascript use an event-based programming model, so you create a (TCP) socket, listen on an IP:port (similar to bind), then accept connection events which pass a Javascript object representing the connection.
From this you can get the FD or another identifier, but this object is also a long-lived object that you can store an identifier on if you wish (this is what socket.io does).
var server = net.createServer();
server.on('connection', function(conn) {
conn.id = Math.floor(Math.random() * 1000);
conn.on('data', function(data) {
conn.write('ID: '+conn.id);
});
});
server.listen(3000);
Timothy's approach is good, the only thing to mention - Math.random() may cause id's duplication. So the chance it will generate the same random number is really tiny, but it could happen. So I'd recommend you to use dylang's module - shortid:
var shortid = require('shortid');
var server = net.createServer();
server.on('connection', function(conn) {
conn.id = shortid.generate();
conn.on('data', function(data) {
conn.write('ID: '+conn.id);
});
});
server.listen(3000);
So in that case you can be sure that no id duplications will occur.
in typescript:
import { v4 as uuidv4 } from 'uuid';
import net from 'net';
class Socket extends net.Socket {
id?: string;
}
const server = net.createServer();
server.on('connection', (conn) => {
conn.id = uuidv4();
conn.on('data', (data) => {
console.log(conn.id);
});
});
server.listen(3000);
you need to add id first;
In c++ to identify a socket we could have done something like writing
a main socket say server to listen for new connections and then
handling those connections accordingly.but so far i havent found
anything like that in node.js . (the berkeley socket model) Does it
even exist in node.js .. if not i am going back to my C++ :$
You should go back, because JavaScript is a prototype-based, object-oriented scripting language that is dynamic, weakly typed and has first-class functions. They are both completely different languages and you will have to have a different mindset to write clean JavaScript code.
https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7
Session ID
If you made use of the sessionId property of socket in v0.6, this is now simply .id.
// v0.6.x
var sid = socket.sessionId;
// v0.7.x
var sid = socket.id;
if you found this question by looking for socket.io unique ids that you can use to differentiate between sockets on the client-side (just like i did), then here is a very simple answer:
var id = 0; //initial id value
io.sockets.on('connection', function(socket) {
var my_id = id; //my_id = value for this exact socket connection
id++; //increment global id for further connnections
socket.broadcast.emit("user_connected", "user with id " + my_id + "connected");
}
on every new connection the id is incremented on the serverside. this guarantees a unique id.
I use this method for finding out where a broadcast came from on the clientside and saving data from concurrent sockets.
for example:
server-side
var my_coords = {x : 2, y : -5};
socket.broadcast.emit("user_position", {id: my_id, coord: my_coords});
client-side
user = {};
socketio.on("user_position", function(data) {
if(typeof user[data.id] === "undefined")
user[data.id] = {};
user[data.id]["x"] = data.coord.x;
user[data.id]["y"] = data.coord.y;
});
How to identify a client based on its socket id. Useful for private messaging and other stuff.
Using socket.io v1.4.5
client side:
var socketclientid = "john"; //should be the unique login id
var iosocket = io.connect("http://localhost:5000", {query: "name=john"});
var socketmsg = JSON.stringify({
type: "private messaging",
to: "doe",
message: "whats up!"
});
iosocket.send(socketmsg);
server side:
io.on('connection', function(socket){
var sessionid = socket.id;
var name = socket.handshake.query['name'];
//store both data in json object and put in array or something
socket.on('message', function(msg){
var thesessionid = socket.id;
var name = ???? //do lookup in the user array using the sessionid
console.log("Message receive from: " + name);
var msgobject = JSON.parse(msg);
var msgtype = msgobject.type;
var msgto = msgobject.to;
var themessage = msgobject.message;
//do something with the msg
//john want to send private msg to doe
var doesocketid = ???? //use socket id lookup for msgto in the array
//doe must be online
//send to doe only
if (msgtype == "private messaging")
socket.to(doesocketid).emit('message', 'themessage');
});
mmmm, i don't really get what you're looking for but socket-programming with node.js (and socket.io) is really straight forward. take a look at some examples on the socket.io homepage:
// note, io.listen() will create a http server for you
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
io.sockets.emit('this', { will: 'be received by everyone connected'});
socket.on('private message', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('disconnect', function () {
sockets.emit('user disconnected');
});
});
on connecting to the server, every socket get an unique id with which you can identify it later on.
hope this helps!?
cheers