node.js - index.js to share mysql details - with page.js? -
index.js has mysql connection details
page.js able use these details
not have provide mysql details
page.js again.
also.. page.js 's output needs made
available index.js index.js can see the
mysql query results.
index.js:
var http = require('http'); var url = require('url'); var mysql = require('mysql'); var connection = mysql.createconnection({ host : '-------------', user : '-----', password : '-------', database : '-----', }); var server=http.createserver(function(req,res){ res.writehead(200,{'content-type': 'text/html; charset=utf-8'}); require('page.js); res.end('test'); }).listen(80);
page.js:
connection.connect(); var querystring = 'select * t1 order id desc limit 5'; connection.query(querystring, function(err,res,fields){ bb = json.stringify(res); }); connection.end();
in page.js
:
exports.makequery = function(connection, callback) { connection.connect(); var querystring = 'select * t1 order id desc limit 5'; connection.query(querystring, function(err,res,fields){ connection.end(); if (err) {return callback(err)}; bb = json.stringify(res); callback(null, bb); }); }
in other:
var http = require('http'); var url = require('url'); var mysql = require('mysql'); var page = require('page.js'); //note here var connection = mysql.createconnection({ host : '-------------', user : '-----', password : '-------', database : '-----', }); var server=http.createserver(function(req,res){ res.writehead(200,{'content-type': 'text/html; charset=utf-8'}); page.makequery(connection, function(err, result){ if(err) return res.end(err); res.end(result); }); }).listen(80);
Comments
Post a Comment