// Library All
var express = require('express');
var app = express();
var mongo = require('mongodb');
app.set('view engine','ejs');
app.use(express.static(__dirname));
// Function All
var func = require("./function.js");
app.get('/index', function (req, res) {
// ส่วนตอนเรียก ------------------------------------- คือปัญหา
func.connect_db();
func.select_cl();
res.render('index.ejs');
});
var server = app.listen(1000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
// Library All
var express = require('express');
var app = express();
app.set('view engine','ejs');
app.use(express.static(__dirname));
// Call Mongo DB
var mongo = require("./mongo.js");
mongo.connect();
app.get('/', function (req, res) {
mongo.getdb().collection("info").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
});
res.render('index.ejs');
});
var server = app.listen(1000, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
mongo.js
//Config DB
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var db_name = "test";
// System DB
var odb = "";
var database = "";
module.exports = {
connect: function() {
MongoClient.connect(url,{ useNewUrlParser: true }, function(err, db) {
if (err) throw err;
odb = db;
database = db.db(db_name);
});
},
disconnect: function() {
odb.close();
},
getdb: function() {
return database;
}
};