我正在创建一个Web应用程序,它首先允许用户使用存储在mysql数据库中的凭据进行登录。 然后,当他们转到/ crm时,我试图传递一个字段值,用于指定用户可以访问的数据库,以实例化bookshelf / knex数据库连接。
crmDB.js
var knex= require('knex')({
client: 'mysql',
connection:{
host: 'localhost', // your host
user: 'the user', // your database user
password: 'the password, // your database password
database: req.user.db, // the database name per the user.db field
charset: 'UTF8_GENERAL_CI'
}
});
var Bookshelf = require('bookshelf')(knex);
module.exports.crmDB = Bookshelf;
我能这样做吗?我的路线是什么?
var crm = function (req, res, next) {
if(req.isAuthenticated()){
} else {
res.redirect('/signin');
}
}
答案 0 :(得分:0)
在route.js
var crm = function (req, res, next) {
if(req.isAuthenticated()){
var user = req.user;
module.exports = {
user:user
};
var crmModel = require('../server/models/crm');
} else {
res.redirect('/signin');
}
}
然后在crmDB.js
中var db = require('../../client/routes').user.toJSON().db;
var knex= require('knex')({
client: 'mysql',
connection:{
host: 'localhost', // your host
user: 'user', // your database user
password: 'password', // your database password
database: db,
charset: 'UTF8_GENERAL_CI'
}
});