我正在尝试将我的第一个MongoDB,Express,websockets应用程序部署到Heroku,但无法连接

时间:2020-09-25 09:47:56

标签: javascript mongodb express heroku websocket

** 我添加了一个Procfile,添加了ObjectRocket插件,并在那里创建了一个数据库。 这是Procfile中的内容 **

 ```
    web: node server.js
    ```

.env文件中的内容 *

```
ORMONGO_URL='mongodb://iad2-c13-0.mongo.objectrocket.com:53577'
ORMONGO_REGION=IAD
ORMONGO_RS_URL='mongodb://iad2-c13-0.mongo.objectrocket.com:53577,iad2-c13- 
2.mongo.objectrocket.com:53577,iad2-c13-1.mongo.objectrocket.com:53577/? 
replicaSet=5df3e347a4384bf8968b430ec021a64f'
PORT = 8000
```

这是我的server.js,尝试连接到ObjectRocket附加数据库: *

   ```


 *
    WEB/HTTP SERVER
    *
    ```
    const mongodb = require("mongodb");
    const bodyParser = require("body-parser");
    const session = require("express-session");
    const MongoStore = require("connect-mongo")(session);
    const path = require("path");
    const express = require("express");
    const app = express();
    const MongoClient = require("mongodb").MongoClient;
    ```

在这里我尝试创建连接字符串

```
    var username = "james";
    var password = "websockets";
    var hosts =
      "iad2-c13-0.mongo.objectrocket.com:53577,iad2-c13-2.mongo.objectrocket.com:53577,iad2-c13- 
    1.mongo.objectrocket.com:53577";
    var database = "websockets";
    var options = "?replicaSet=5df3e347a4384bf8968b430ec021a64f";
    var connectionString =
      "mongodb://" +
      username +
      ":" +
      password +
      "@" +
      hosts +
      "/" +
      database +
      options;
 

在这里我为帕格和文件设置快递

    app.use("/public", express.static(__dirname + "/public"));
   
    app.set("view engine", "pug");
    
    app.use(
      bodyParser.urlencoded({
        extended: true,
      })
    );
    ```

首次尝试将用户添加到数据库

 ```      
app.get("/", (request, response, next) => {
  response.render("signup");
});


app.post("/signup", (request, response, next) => {
  request.body.name;
  request.body.email;
  request.body.password;
  mongodb.MongoClient.connect(connectionString, (error, client) => {
    if (error) {
      console.log(error);
      response.redirect("/signup");
    } else {
      const db = client.db("websockets");
      db.collection("users", (error, collection) => {
        collection.insertOne(
          {
            name: request.body.name,
            email: request.body.email,
            password: request.body.password,
          },
          (error, result) => {
            if (error) {
              response.redirect("/signup");
            }
          }
        );
      });
      response.redirect("/login");
    }
  });
});
```
     

这里是错误日志

 PS C:\Users\James\Desktop\Atelier_back\pictionary_p5> heroku logs --tail    
 »   Warning: heroku update available from 7.42.4 to 7.43.2.
2020-09-25T10:59:28.815676+00:00 heroku[router]: at=info method=GET path="/signup" host=evening-coast-84225.herokuapp.com request_id=ab3d1667-52ae-493b-b3a1-5d4849879b71 fwd="91.166.35.81" dyno=web.1 connect=0ms service=6ms status=200 bytes=723 protocol=https
2020-09-25T10:59:28.959611+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=f4ae2a13-4c15-480b-b109-c177626ab743 fwd="91.166.35.81" dyno=web.1 connect=0ms service=17ms status=304 bytes=237 protocol=https        
2020-09-25T10:59:30.952533+00:00 heroku[router]: at=info method=GET path="/login" host=evening-coast-84225.herokuapp.com request_id=35d8b3e2-544c-4aa1-b771-d2f5afc035a4 fwd="91.166.35.81" dyno=web.1 connect=0ms service=32ms status=200 bytes=648 protocol=https
2020-09-25T10:59:31.087075+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=c57919a7-5e16-455c-95f9-96f791629a15 fwd="91.166.35.81" dyno=web.1 connect=0ms service=4ms status=304 bytes=237 protocol=https
2020-09-25T10:59:36.419574+00:00 heroku[router]: at=info method=POST path="/login" host=evening-coast-84225.herokuapp.com request_id=65bfc81e-84f9-4545-8922-05792448b74e fwd="91.166.35.81" dyno=web.1 connect=0ms service=127ms status=302 bytes=249 protocol=https
2020-09-25T10:59:36.524540+00:00 heroku[router]: at=info method=GET path="/login" host=evening-coast-84225.herokuapp.com request_id=89f6b294-f2e9-4b44-a95e-6142bad05433 fwd="91.166.35.81" dyno=web.1 connect=0ms service=2ms status=200 bytes=648 protocol=https
2020-09-25T10:59:36.651733+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=6fc746f3-14bc-4d27-9b9c-6d669f582b62 fwd="91.166.35.81" dyno=web.1 connect=0ms service=2ms status=304 bytes=237 protocol=https
2020-09-25T10:59:51.074822+00:00 heroku[router]: at=info method=POST path="/login" host=evening-coast-84225.herokuapp.com request_id=497437d8-7f0e-4da5-9a4f-a646274a4c15 fwd="91.166.35.81" dyno=web.1 connect=1ms service=79ms status=302 bytes=249 protocol=https
2020-09-25T10:59:51.196106+00:00 heroku[router]: at=info method=GET path="/login" host=evening-coast-84225.herokuapp.com request_id=f3bb3025-06be-4cd0-9c36-c90b5ec8cb09 fwd="91.166.35.81" dyno=web.1 connect=1ms service=18ms status=200 bytes=648 protocol=https
2020-09-25T10:59:51.327253+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=d1185dcb-eded-4314-b54b-783c03ddde2b fwd="91.166.35.81" dyno=web.1 connect=0ms service=5ms status=304 bytes=237 protocol=https
2020-09-25T11:00:01.409973+00:00 heroku[router]: at=info method=GET path="/signup" host=evening-coast-84225.herokuapp.com request_id=b4c79f5d-966a-4696-8bdd-4d32c6bb07f3 fwd="91.166.35.81" dyno=web.1 connect=0ms service=6ms status=304 bytes=151 protocol=https
2020-09-25T11:00:01.581572+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=7e0cf022-3ddc-485e-99fb-0f8064f9dd24 fwd="91.166.35.81" dyno=web.1 connect=0ms service=32ms status=304 bytes=237 protocol=https        
2020-09-25T11:00:08.832547+00:00 app[web.1]: MongoNetworkError: failed to connect to server [iad2-c13-2.mongo.objectrocket.com:53577] on first connect [MongoError: Authentication failed.
2020-09-25T11:00:08.832561+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:19)
2020-09-25T11:00:08.832563+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:08.832564+00:00 app[web.1]: at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:452:10)        
2020-09-25T11:00:08.832564+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:621:15)    
2020-09-25T11:00:08.832565+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-09-25T11:00:08.832565+00:00 app[web.1]: at addChunk (_stream_readable.js:295:12)
2020-09-25T11:00:08.832567+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:271:9)
2020-09-25T11:00:08.832567+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:212:10)
2020-09-25T11:00:08.832568+00:00 app[web.1]: at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
2020-09-25T11:00:08.832569+00:00 app[web.1]: ok: 0,
2020-09-25T11:00:08.832570+00:00 app[web.1]: code: 18,
2020-09-25T11:00:08.832570+00:00 app[web.1]: codeName: 'AuthenticationFailed',
2020-09-25T11:00:08.832571+00:00 app[web.1]: operationTime: [Timestamp],
2020-09-25T11:00:08.832572+00:00 app[web.1]: '$clusterTime': [Object]
2020-09-25T11:00:08.832572+00:00 app[web.1]: }]
2020-09-25T11:00:08.832573+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-09-25T11:00:08.832573+00:00 app[web.1]: at Pool.emit (events.js:315:20)
2020-09-25T11:00:08.832573+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-09-25T11:00:08.832574+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-09-25T11:00:08.832574+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:75:5)
2020-09-25T11:00:08.832575+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:147:27
2020-09-25T11:00:08.832575+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/auth/scram.js:108:14
2020-09-25T11:00:08.832576+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connection.js:329:7)
2020-09-25T11:00:08.832576+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:9)
2020-09-25T11:00:08.832577+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:08.835653+00:00 heroku[router]: at=info method=POST path="/signup" host=evening-coast-84225.herokuapp.com request_id=a1833b19-b344-4e98-90da-68135aad5b9a fwd="91.166.35.81" dyno=web.1 connect=0ms service=72ms status=302 bytes=252 protocol=https
2020-09-25T11:00:08.954095+00:00 heroku[router]: at=info method=GET path="/signup" host=evening-coast-84225.herokuapp.com request_id=e5dbd4b0-e3f3-4b09-9a39-ef82a692f476 fwd="91.166.35.81" dyno=web.1 connect=1ms service=8ms status=200 bytes=723 protocol=https
2020-09-25T11:00:09.082458+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=4b237eb1-edee-4e09-86f5-9d6e3b725f17 fwd="91.166.35.81" dyno=web.1 connect=0ms service=3ms status=304 bytes=237 protocol=https
2020-09-25T11:00:10.261510+00:00 app[web.1]: MongoNetworkError: failed to connect to server [iad2-c13-1.mongo.objectrocket.com:53577] on first connect [MongoError: Authentication failed.
2020-09-25T11:00:10.261525+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:19)
2020-09-25T11:00:10.261525+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:10.261526+00:00 app[web.1]: at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:452:10)
2020-09-25T11:00:10.261527+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:621:15)
2020-09-25T11:00:10.261527+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-09-25T11:00:10.261527+00:00 app[web.1]: at addChunk (_stream_readable.js:295:12)
2020-09-25T11:00:10.261528+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:271:9)
2020-09-25T11:00:10.261528+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:212:10)
2020-09-25T11:00:10.261529+00:00 app[web.1]: at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
2020-09-25T11:00:10.261530+00:00 app[web.1]: ok: 0,
2020-09-25T11:00:10.261531+00:00 app[web.1]: code: 18,
2020-09-25T11:00:10.261531+00:00 app[web.1]: codeName: 'AuthenticationFailed',
2020-09-25T11:00:10.261531+00:00 app[web.1]: operationTime: [Timestamp],
2020-09-25T11:00:10.261532+00:00 app[web.1]: '$clusterTime': [Object]
2020-09-25T11:00:10.261532+00:00 app[web.1]: }]
2020-09-25T11:00:10.261533+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-09-25T11:00:10.261533+00:00 app[web.1]: at Pool.emit (events.js:315:20)
2020-09-25T11:00:10.261533+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-09-25T11:00:10.261534+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-09-25T11:00:10.261534+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:75:5)
2020-09-25T11:00:10.261535+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:147:27
2020-09-25T11:00:10.261535+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/auth/scram.js:108:14
2020-09-25T11:00:10.261535+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connection.js:329:7)
2020-09-25T11:00:10.261536+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:9)
2020-09-25T11:00:10.261536+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:10.264445+00:00 heroku[router]: at=info method=POST path="/signup" host=evening-coast-84225.herokuapp.com request_id=521aaf6d-7d63-4d15-8c8d-c3a8c8c1ea31 fwd="91.166.35.81" dyno=web.1 connect=2ms service=54ms status=302 bytes=252 protocol=https
2020-09-25T11:00:10.370377+00:00 heroku[router]: at=info method=GET path="/signup" host=evening-coast-84225.herokuapp.com request_id=e95baff4-d58c-479a-ab5c-95cd3163b72a fwd="91.166.35.81" dyno=web.1 connect=0ms service=4ms status=200 bytes=723 protocol=https
2020-09-25T11:00:10.502587+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=1958ab4f-ccbf-4c86-a803-b9a75877acde fwd="91.166.35.81" dyno=web.1 connect=0ms service=6ms status=304 bytes=237 protocol=https
2020-09-25T11:00:11.181066+00:00 app[web.1]: MongoNetworkError: failed to connect to server [iad2-c13-1.mongo.objectrocket.com:53577] on first connect [MongoError: Authentication failed.
2020-09-25T11:00:11.181084+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:19)
2020-09-25T11:00:11.181086+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:11.181088+00:00 app[web.1]: at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:452:10)        
2020-09-25T11:00:11.181089+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:621:15)    
2020-09-25T11:00:11.181089+00:00 app[web.1]: at Socket.emit (events.js:315:20)
2020-09-25T11:00:11.181090+00:00 app[web.1]: at addChunk (_stream_readable.js:295:12)
2020-09-25T11:00:11.181091+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:271:9)
2020-09-25T11:00:11.181091+00:00 app[web.1]: at Socket.Readable.push (_stream_readable.js:212:10)
2020-09-25T11:00:11.181092+00:00 app[web.1]: at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
2020-09-25T11:00:11.181093+00:00 app[web.1]: ok: 0,
2020-09-25T11:00:11.181094+00:00 app[web.1]: code: 18,
2020-09-25T11:00:11.181094+00:00 app[web.1]: codeName: 'AuthenticationFailed',
2020-09-25T11:00:11.181096+00:00 app[web.1]: operationTime: [Timestamp],
2020-09-25T11:00:11.181096+00:00 app[web.1]: '$clusterTime': [Object]
2020-09-25T11:00:11.181096+00:00 app[web.1]: }]
2020-09-25T11:00:11.181097+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-09-25T11:00:11.181097+00:00 app[web.1]: at Pool.emit (events.js:315:20)
2020-09-25T11:00:11.181098+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-09-25T11:00:11.181098+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-09-25T11:00:11.181098+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:75:5)
2020-09-25T11:00:11.181099+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:147:27
2020-09-25T11:00:11.181099+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/auth/scram.js:108:14
2020-09-25T11:00:11.181100+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connection.js:329:7)
2020-09-25T11:00:11.181100+00:00 app[web.1]: at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connection.js:360:9)
2020-09-25T11:00:11.181100+00:00 app[web.1]: at Connection.emit (events.js:315:20)
2020-09-25T11:00:11.181770+00:00 heroku[router]: at=info method=POST path="/signup" host=evening-coast-84225.herokuapp.com request_id=9d65540b-8132-416b-95f1-61cac24d4810 fwd="91.166.35.81" dyno=web.1 connect=0ms service=67ms status=302 bytes=252 protocol=https
2020-09-25T11:00:11.296931+00:00 heroku[router]: at=info method=GET path="/signup" host=evening-coast-84225.herokuapp.com request_id=c77ce66c-2d50-4906-ae85-f3e2309288df fwd="91.166.35.81" dyno=web.1 connect=0ms service=8ms status=200 bytes=723 protocol=https
2020-09-25T11:00:11.429086+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=b131eaf2-e48e-462e-84d4-8f7b4d51aa1c fwd="91.166.35.81" dyno=web.1 connect=0ms service=3ms status=304 bytes=237 protocol=https
2020-09-25T11:00:13.161144+00:00 heroku[router]: at=info method=GET path="/public/style.css" host=evening-coast-84225.herokuapp.com request_id=b1ed0cde-00e7-4782-b32f-d04da727910f fwd="91.166.35.81" dyno=web.1 connect=1ms service=5ms status=304 bytes=237 protocol=https

0 个答案:

没有答案