PHP to node.js

211 views
Skip to first unread message

Vasa

unread,
Nov 18, 2014, 2:51:53 AM11/18/14
to nod...@googlegroups.com
Hello noders

i am trying to generate a chart (using highcharts) with queried data from Mysql. 
I have my data.php file to query the database and create a JSON file. This JSON file is used by index.php file to draw the chart. 
This is not working because the chart is drawn before the sql data is retrieved. 

I am trying to create a nodejs for the same and i assume this is the solution to my requirement

Any advice here




data.php
index.php

Aredridel

unread,
Nov 18, 2014, 10:02:27 AM11/18/14
to Vasa, nod...@googlegroups.com

On Nov 18, 2014 2:51 AM, Vasa <vijayaraj...@gmail.com> wrote:
>
> Hello noders
>
> i am trying to generate a chart (using highcharts) with queried data from Mysql. 

[Snip]

> This is not working because the chart is drawn before the sql data is retrieved. 

This is common as people start getting going in node. Move the drawing parts into the callback for the SQL query, so it waits until the data arrives.

Unlike php, request handlers don't just run to the bottom and stop. Instead, the top level is run which sets things up, which load data, which set things up and finally respond. Http responses aren't done until red.end(). That can be delayed as much as you need.

Next, you'll start nesting deeply, so http://callbackhell.com will be a short, good read for you.

Good luck!

Aria

Vasa

unread,
Nov 19, 2014, 1:35:12 AM11/19/14
to nod...@googlegroups.com
Hello Aria,

Thank you for pointing out the bug. I am new to PHP :).  I fixed it and my PHP is working fine retrieving the data and charting. 
But i am trying to move the db queries to my nodejs. So that when a request is made, it queries the db and then tries to chart the data. 
I have attached my node server. Please help me out.   
webserver.js

Christopher Rust

unread,
Nov 19, 2014, 1:09:11 PM11/19/14
to nod...@googlegroups.com

Every time you have a function that you want to run first, that makes use of callbacks, such as:

myFunc(function(err, result) { ... });

Then you need to place the code you want to run second where ... is. This is the asynchronous nature of node. Any code where ... is will not run until myFunc is finished internally and calls it's argument function. If there's another synchronous function, meaning it doesn't accept a function as a parameter, outside of myFunc it will be run first. Otherwise they will be driven in order of the events caused by the user.

dbclient.query() needs to be inside of the anonymous (read: unnamed) function that's passed to dbclient.connect()

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cc403d67-c255-4edd-9a32-0fc6868f23af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

// ravi

unread,
Nov 19, 2014, 1:09:28 PM11/19/14
to nod...@googlegroups.com
On Nov 19, 2014, at 1:35 AM, Vasa <vijayaraj...@gmail.com> wrote:

Thank you for pointing out the bug. I am new to PHP :).  I fixed it and my PHP is working fine retrieving the data and charting.  But i am trying to move the db queries to my nodejs. So that when a request is made, it queries the db and then tries to chart the data. I have attached my node server. Please help me out.
 

Vasa,

I am curious why you have or need both PHP and NodeJS? Ignore this if you have already proceeded past that point and Aria might have a quick answer for your question above.

Is there a typo in the above? Did you mean NodeJS where you write PHP, above?

—ravi

zladuric

unread,
Nov 20, 2014, 3:00:45 AM11/20/14
to nod...@googlegroups.com
Vasa,

It seems your webserver.js is incomplete? Either that or you forgot to call res.end() in there.
From what I see, you read file names the database? Then read those files? What do you want to do with them? Let's say you just want to respond with a JSON of the files you found in the directories you queried.

You'd have to make a few changes to webserver.js.

1. in your request handler, you have an `if(err){return;}` block. You should do something like `if(err) { res.status(500); res.end(err);}`. That would close the request (that is still waiting for you to end it after you went to the database).

2. After that if block, you should do something with results.  Like, res.json(files);. That would send back the response as  content-type application/json and end the request.

Hope that helps.

Vasa

unread,
Nov 20, 2014, 8:24:58 AM11/20/14
to nod...@googlegroups.com
Thank you for your reply. 

Vasa

unread,
Nov 20, 2014, 8:43:12 AM11/20/14
to nod...@googlegroups.com
Ravi

I am developing an application that queries mysql database (that gets updated realtime) whenever there is a http request and draws a chart with the data.  Basically i am POPULATING DATA USING SERVER SIDE CODE. I found a sample in php which works fine for small database. But it fails for large databases. The chart displays nothing for large data. So i am trying to create a nodejs(asynchronous) which is the solution. I have provided the php files and my webserver.js(nodejs). I never mentioned that my webserver.js is final. It has method stubs that needs to be developed. I am new to nodejs, PHP or for that matter Web applications. I am trying to learn while developing my application. Have i made myself clear. Can you help me out with the code  

Vasa

unread,
Nov 20, 2014, 8:49:33 AM11/20/14
to nod...@googlegroups.com
Hello zladuric,

Thank you for your inputs. I know my webserver.js (which is my nodejs file) is incomplete. I have just included method stubs that needs to be developed. 
 
Alright, let me explain what i am trying to do. 

          I am developing an application that queries mysql database (that gets updated realtime) whenever 
          there is a http request and draws a chart with the data.  Basically i am POPULATING DATA USING SERVER 
          SIDE CODE. I found a sample in php which works fine for small database. But it fails for large databases. 
          The chart displays nothing for large data. So i am trying to create a nodejs(asynchronous) which is the solution. 

Can you make the changes in the webserver.js file and post it here. That would be of great help to any beginner here. Thanks again

Vasa

unread,
Nov 20, 2014, 9:11:15 AM11/20/14
to nod...@googlegroups.com
Please ignore the older webserver.js file. This is the updated one with method stubs that needs to be developed


On Thursday, November 20, 2014 1:30:45 PM UTC+5:30, zladuric wrote:
webserver.js

Oliver Leics

unread,
Nov 21, 2014, 2:55:24 AM11/21/14
to nod...@googlegroups.com
Hi Vasa,

https://gist.github.com/oleics/36fd5049cf2f848e34a5

It's not working and I will not finish this gist, but you can use it
as a starting point, if you want to.

Two random notes:

* I see no need to require fs or socket.io. Stick to the minimum you
need, everything else is just confusing, even if require('socket.io')
might look cool ^^
* Express is a shortcut for the http-module, a very very handy shortcut.

Have fun!
> https://groups.google.com/d/msgid/nodejs/2b97a7dc-5ffe-496c-8ccf-8b6d684ee2b6%40googlegroups.com.

Vasa

unread,
Nov 24, 2014, 12:24:43 AM11/24/14
to nod...@googlegroups.com
Hello Oliver,

Thank you for the code. This is really helpful. 

About the socketio :) i do need it in my application. I am trying to implement a chart that is real-time.
nodejs server connects to my database using socket io. 
Reply all
Reply to author
Forward
0 new messages