javascript - NodeJs, pattern for sync developement -



javascript - NodeJs, pattern for sync developement -

i new nodejs , express frameworks. have understood node works 1 thread on server side. so, have noticed causes me problems in order develop correctly application.

in routes folder, have file index.js. file manage navigation asked user app.js.

so decided create route function "test". in function, had code

exports.test = function(req, res){ res.render('test', {}); };

so simple, easy. that's rend template test.jade in views folder. greats ! wanna complexify process. in test route function, want load content mysql database.

for that, have created folder models in folders node_modules inside, have 2 file, first mysqlconnection.js exports variable db in order create queries.

var mysql = require('mysql'); var db = mysql.createconnection( { host : 'localhost', user : 'root', password : '', database : 'test', } ); db.connect(); module.exports = db;

in sec file, articles_class.js, have

var db = require('models/mysqlconnection'); var article = function() { this.getarticles = function() { db.query('select * articles;', function(err, rows, fields) { if (err) throw err; else { console.log(rows); homecoming (rows); } }); } } module.exports = article;

go in route test function :

i want load table "test" articles. basic. not easy. why ? because before query finished, nodejs respond client template render, but, unfornlty, without rows loaded. asynchronous problem ... mysql doesn't block nodejs javascript instruction.

the code of function :

exports.test = function(req, res){ var article = require('models/articles_class'); = new article(); articles = a.getarticles(); console.log(articles); // undefined res.render('test', {}); };

i found others subjects in stackoverflow speak problem. create sync queries, work callbacks ect .. for, here, if seek manage problem callbacks, that's cannot work ... because need send client template articles can't block process sync method.

i lost ... don't understand how have build application. not able create proceed in order manage sql queries. there pattern or specific method ?

or perhaps have create ajax requests client. load template "test". , in javascript file in public folder, inquire server load me articles content , wait success callback function ? it's not clean ...

thx answers. others answers have found didn't help me understand how manage nodejs.

pass callback getarticles:

exports.test = function(req, res){ var article = require('models/articles_class'); = new article(); a.getarticles( function( articles ) { console.log(articles); // undefined res.render('test', { articles: articles }); }); };

changes articles function:

var db = require('models/mysqlconnection'); var article = function() { this.getarticles = function( callback ) { db.query('select * articles;', function(err, rows, fields) { if (err) throw err; else { console.log(rows); callback && callback(rows); } }); } } module.exports = article;

express homecoming template through open http connection 1 time res.render() called. it's matter of passing callback through phone call stack, should called after have database rows.

as working callbacks, don't block application.

javascript mysql node.js express sync

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -