如何在node express应用程序周围传递对象?

时间:2013-04-17 06:06:33

标签: node.js postgresql module express

我正在使用express和node-postgres(https://github.com/brianc/node-postgres)构建节点应用程序。我只想构建一次db客户端连接,我希望能够从不同的模块访问这个db连接。做这个的最好方式是什么?我试图只导出数据库连接,而不是整个快递应用程序。从本质上讲,跨节点应用程序导出和访问对象的最佳方法是什么?

我已经检查了这个类似的问题,但它似乎特别适用于猫鼬。

Best way to share database connection param with mongoose/node.js

3 个答案:

答案 0 :(得分:5)

没有一种叫做“最好的方式”的东西。如果需要在不同模块中使用相同的对象,则必须将其包装在模块中。像这样:

//db.js
var postgres = require (...)
var connection;

module.exports = {
  getConnection: function (){
    return connection;
  },
  createConnection: function (){
    connection = createConnection (postgress);
  }
};

//app.js - main file
require ("./db").createConnection ();

//a.js
var db = require("./db")
db.getConnection()

//b.js
var db = require("./db")
db.getConnection()

答案 1 :(得分:-1)

你可以做那样的事情..

//db.js
var pg = require('pg'); 

var conString = "tcp://postgres:1234@localhost/postgres";

module.exports.connectDatabase = function(callback){
var client = new pg.Client(conString);
client.connect(function(err) {
  if(err){
     console.log(err);
     process.exit(1);
  }

  module.exports.client = client;
  callback();
})

//app.js
// We are trying to connect to database at the start of our app and if it fails we exit       the process
var db = require('./db');
db.connectDatabase(function(){
  // your other code
})

//a.js
var db = require('./db');
//you can access your client here to perform database operations like that
db.client

答案 2 :(得分:-1)

HI Im正在为此寻求解决方案,它是从RefLink那里获得的

您可以创建这样的方案以映射数据

    const User = mongoose.model('Story', userSchema);
    module.exports = User;
    const mongoose = require('mongoose');
    let Schema = mongoose.Schema;
    const userSchema = new Schema({
    UserID: {
        type: mongoose.Schema.Types.Mixed,
    },
    User_Info: {
        First_Name: {
        type: String,
        },
        Last_Name: {
        type: String,
        },
        Current_Address: {
        type: String,
        },
        Email_Address: {
        type: String,
        },
    },
        Phone_Numbers: [{
            Home_Phone: {
            type: Number,
            },
            Work_Phone: {
            type: Number,
            },
            Cell_Phone: {
            type: Number,
            },
                Phone_verified: [{
                Home: Boolean,
                Work: Boolean,
                Cell: Boolean,
                }],
        }],
    })
    const User = mongoose.model('User', userSchema);
    module.exports = User;

API路由可能看起来像这样

        app.post('/api/user', function(req, res) {
    User.create({
        UserID: req.body.userid,
        User_Info: req.body.userinfo,
        First_Name: req.body.firstname,
        Last_Name: req.body.lastname,
        Current_Address: req.body.currentaddress,
        Email_Address: req.body.emailaddress,
        Phone_Numbers: req.body.phonenumbers,
        Home_Phone: req.body.homephone,
        Work_Phone: req.body.workphone,
        Cell_Phone: req.body.cellphone,
        Phone_Verified:
        req.body.phoneverified,
        Home: req.body.home,
        Work: req.body.work,
        Cell: req.body.cell,
    }).then(user => {
        res.json(user)
    });
    });