you have one global connection which you close after the first request is fulfilled, do not close the connection in the request handlers or use a connection pool and grab a connection for each request separately
MK On Tue, Jun 11, 2013 at 9:21 AM, Giuseppe <[email protected]> wrote: > Hi all, > > We need to develop a little restful server to return some records from a > Mysql database in JSON. > > I started to read about nodejs, and looks great, but I have a doubt I hope > you can help me. > > I'm creating in my app.js differents methods to answer different > petitions, the problem is, I can call more than once a because I get > > Error: Cannot enqueue Handshake after invoking quit. >> >> > I readed about the problem is I need to createConnection every time I do > connectiont.end() but I would like to do it with a function or something > else, to avoit to rewrite the complete createConnection in every GET > method. I will have about 60, and don't want to rewrite all if something > changes. My JS skills are not very good, this could be my first serious JS > approach > > This is my actual code. > > var express = require('express'); >> >> var mysql = require('mysql'); >> >> >>> var app = express(); >> >> >>> var conexion = mysql.createConnection({ >> >> host : 'localhost', >> >> user : 'root', >> >> password: '', >> >> database: 'databasename' >> >> }); >> >> >>> conexion.connect(); >> >> >>> //Recoge contenido categorÃa Actualidad, id 26 >> >> app.get('/getActualidad', function(req,res) { >> >> var contenido = conexion.query('SELECT * FROM jos_content WHERE >>> catid=26 ORDER BY created desc LIMIT 10', function err(err, results, >>> fields) { >> >> if (err) { >> >> console.log("Error: " + err.message); >> >> throw err; >> >> } >> >> console.log("Number of rows: "+results.length); >> >> res.json(results); >> >> conexion.end(); >> >> }); >> >> }); >> >> >>> app.get('/elcolegio/:id', function(req,res) { >> >> console.log(req.params.id); >> >> var contenido = conexion.query( >> >> 'SELECT * FROM jos_content WHERE id=' + req.params.id, >> >> function err(err, results, fields) { >> >> if (err) { >> >> console.log("Error: " + err.message); >> >> throw err; >> >> } >> >> console.log("Number of rows: "+results.length); >> >> res.json(results); >> >> conexion.end(); >> >> }); >> >> }); >> >> >>> app.get('/', function(req,res) { >> >> res.type('text/plain'); >> >> res.send('Probando rest nodejs'); >> >> }); >> >> >>> app.listen(process.env.PORT || 8123); >> >> > Thanks a lot in advance. > > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > 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 post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > 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 [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: 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 post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- 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 [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
