如何在Digitalocean中的相同(单个)Droplet中将MongoDB与Node.js连接

时间:2019-07-06 18:05:05

标签: node.js mongodb mongoose digital-ocean mongoose-schema

我试图在同一分支中连接mongodb和节点服务器api应用(请求URL https://services.suriyashopping.com/api/menu-list)。 nodejs服务器应用程序和mongodb运行在单个Droplet中,服务器放置在子域中。我在下面提到了我的数据库连接代码,并且在通过“ nodemon app.js”运行服务器时获得了成功连接。但是之后,我从客户端向服务器发出了请求,然后出现一些未授权的错误。还有一个查询是为什么在通过Ubuntu终端“ nodemon app.js”运行服务器时,数据库和相关集合没有自动生成。我已经使用nginix服务器块指向子域。如果我在本地计算机数据库中运行服务器,则会生成相关集合。但这不是生成的。

NodeJs,Express,mongodb,mongoose,linux 16.04,nginix用于服务器块指向子域

MongoDB连接代码

我尝试了Droplet ip作为本地IP“ 127.0.0.1”,也尝试了我的Droplet“ 142.93.215.18”

mongoose.connect('mongodb://dbuser:dbpassword@dropletip:27017/SURIYAShoppingDB', 
  { useNewUrlParser: true }, (err, res) => {
        if(err) {
      console.log(err);
    }
    else {
      console.log('MongoDB connection succeeded.');
    }
  })

预期结果

我尝试了此请求网址以获取响应(如果有)  https://services.suriyashopping.com/api/menu-list

实际结果

{ MongoError: not authorized on SURIYAShoppingDB to execute command { find: "maincategories", filter: { status: "true", active_status: "true" }, projection: { __v: false, catgoryImage: false, active_status: false, status: false, createdAt: false, updatedAt: false }, returnKey: false, showRecordId: false }
    at Connection.<anonymous> (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/pool.js:443:61)
    at Connection.emit (events.js:198:13)
    at processMessage (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/connection.js:364:10)
    at Socket.<anonymous> (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/connection.js:533:15)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  ok: 0,
  errmsg:
   'not authorized on SURIYAShoppingDB to execute command { find: "maincategories", filter: { status: "true", active_status: "true" }, projection: { __v: false, catgoryImage: false, active_status: false, status: false, createdAt: false, updatedAt: false }, returnKey: false, showRecordId: false }',
  code: 13,
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:28975) DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead

我的UFW状态

mydroplet:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
27017                      ALLOW       142.93.215.18
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

0 个答案:

没有答案