Sümer Kolçak Sümer Kolçak - 14 days ago 7
Node.js Question

node.js - what is "export" all about. i simply want it to "merge"

This works fine but I need to split it. For the purposes of running different codes for different pages on my web site.

var http = require('http');
var url = require('url');
var mysql = require('mysql');

var connection = mysql.createConnection({
host : '-----',
user : '-----',
password : '----',
database : '----',
});

connection.connect();

var ss = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5';

connection.query(ss, function(err,res,fields){
aa = JSON.stringify(res);
});


var server=http.createServer(function(req,res){
res.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
res.end('test' + aa);
}).listen(80);


let's split it into:

1.js and 2.js

1.js:

included this line:

require('/2.js');

var http = require('http');
var url = require('url');
var mysql = require('mysql');

var connection = mysql.createConnection({
host : '-----',
user : '-----',
password : '----',
database : '----',
});

connection.connect();

var server=http.createServer(function(req,res){
res.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
require('/2.js');
res.end('test' + aa);
}).listen(80);


2.js

var ss = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5';

connection.query(ss, function(err,res,fields){
aa = JSON.stringify(res);
});


How can I tell 1.js to only include 2.js if the url pathname is '/2'?

I rather not do "exports" and so on. I want it to simply be like php's "include" concept. It would be more logical because it would be "seamless".

Answer

Node.js's module mechanism does not work like PHP's.

One way of making your code work is like this:

1.js

var http = require('http');
var url = require('url');
var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : '-----',
    user     : '-----',
    password : '----',
    database : '----',
});

connection.connect();

var server = http.createServer(function(req,res){
    var two = require('./2.js');
    two.myQuery(connection, function(err, aa) {
        res.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
        if (err) {
            res.end('error: ' + err);
        }
        else {
            res.end('test' + aa);
        }
    });
}).listen(80);

2.js

function myQuery(connection, cb) {
    var ss = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5';

    connection.query(ss, function(err,res,fields){
        if (err) {
            cb(err);
            return;
        }
        cb(null, JSON.stringify(res));
    });
}

module.exports = {
    myQuery: myQuery
};

This would require that you pass the database connection to each query. Alternatively, you could put the database connection in a separate file, like this.

1.js

var http = require('http');
var url = require('url');
var mysql = require('mysql');

var server = http.createServer(function(req,res){
    var two = require('./2.js');
    two.myQuery(function(err, aa) {
        res.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
        if (err) {
            res.end('error: ' + err);
        }
        else {
            res.end('test' + aa);
        }
    });
}).listen(80);

db.js

var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : '-----',
    user     : '-----',
    password : '----',
    database : '----',
});

connection.connect();

module.exports = {
    connection: connection
}

2.js

var db = require('./db'); 
function myQuery(cb) {
    var ss = 'SELECT * FROM 1_accounts order by ac_nu asc limit 5';

    db.connection.query(ss, function(err,res,fields){
        if (err) {
            cb(err);
            return;
        }
        cb(null, JSON.stringify(res));
    });
}

module.exports = {
    myQuery: myQuery
};

Regardless of the solution you use, I recommend reading more about node.js modules to familiarise yourself with how this mechanism works.