I am new to Node. I have done a sample application where I have done all the code in one file server.js
var express = require('express'),
nconf=require('nconf');
var app = express()
nconf.env().file({ file: 'db-config.json'});
app.use(express.static(__dirname+"\\client"));
var dbConfig = nconf.get();
console.log();
var mysql = require('mysql');
var connection = mysql.createConnection({
host: dbConfig.hostname,
port: dbConfig.port,
user: dbConfig.user,
password: dbConfig.password,
database: dbConfig.db
});
app.get('/', function (req, res) {
res.sendFile(__dirname+"\\client\\index.html");
})
app.get('/getTables', function (req, res) {
var sql="SELECT table_name as text from information_schema.tables where table_schema = ?";
connection.query(sql,[dbConfig.db],function(err,rows,fields){
if(!err){
var data={
"children":[]
};
for(var i=0;i<rows.length;i++){
rows[i].leaf=true;
data.children.push(rows[i]);
}
res.json(data);
}else{
console.log("db not connected");
}
});
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
I want to know how to write all my mysql config code in one file and use where ever i want. And I want to write client response creation in another fie.
requirefunction is there for a reasonexpress jsI recommend you to type in console:npm install express-generator -gthis will create for you template project with initial structure. All folder like routes, views, javascripts and etc. From there you also will see how everytying is connected, but any way you should userequirelike in the answer below.