English 中文(简体)
我如何用不同的蒙戈数据库建立不同的节点 Js 环境?
原标题:How do I set up different node.js environments with different mongo databases?

我想在开发中使用本地的 mongo 服务器, 然后在生产中切换到 mongolab db 。 在节点/ js Express 应用程序中, 推荐的方法是什么? 我只知道 NODE_ ENV 存在, 但不确定根据数据库的价值切换数据库的最佳方法 。 有 :

if (process.env.NODE_ENV == "production") 
{
  //
}
else if ((process.env.NODE_ENV == "development") 
{

}

在所有我查询 db 的地方, Db 似乎并不非常优雅。 是否有方法可以类似铁路应用程序, 您指定用于生产和开发的适配器?

最佳回答

您只需在应用程序启动时使用一个 URL 连接到您的 DB 即可 。 您也可以使用类似代码来根据运行时间环境选择正确的 URL 。 连接后, 您应用程序应该引用一个共享的数据库连接对象, 以正常的方式执行查询和操作 。 您可能想要用一个配置对象来处理这个问题, 正如我在 < a href=> https:// stackoverflow. com/ queseses/57778245/ 表达式js- show- how- to- situal- an- application/73508775# 7350875” >ExpressJS 如何构建一个应用程序?

问题回答

暂无回答




相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签