0

所以,我已经开始学习平均堆栈,当我尝试连接到在沙箱中运行的 mongodb 时,我遇到了这个问题,我的防病毒软件被禁用

这是我的代码,

            const express = require("express");
            const bodyParser = require("body-parser");
            const Post = require("./models/post");
            const mongoose = require("mongoose");

            const app = express();

            mongoose
            .connect(
                "mongodb+srv://bharat:<password>@mongodbcluster-rkdrm.mongodb.net/test?retryWrites=true&w=majority",
                { useNewUrlParser: true }
            )
            .then(() => {
                console.log("Connected to DB");
            })
            .catch(() => {
                console.log("Connection failed!");
            });

            app.use(bodyParser.json());
            app.use(bodyParser.urlencoded({ extended: false }));

            app.use((req, res, next) => {
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader(
                "Access-Control-Allow-Headers",
                "Origin, X-Requested-With, Content-Type, Accept"
            );
            res.setHeader(
                "Access-Control-Allow-Methods",
                "GET, POST, PATCH, DELETE, OPTIONS"
            );
            next();
            });

            app.post("/api/posts", (req, res, next) => {
            const post = new Post({
                title: req.body.title,
                content: req.body.content
            });

            console.log(post);
            res.status(201).json({
                message: "Post added successfully"
            });
            });

            app.get("/api/posts", (req, res, next) => {
            const posts = [
                {
                id: "fadf12421l",
                title: "First server-side post",
                content: "This is coming from the server"
                },
                {
                id: "ksajflaj132",
                title: "Second server-side post",
                content: "This is coming from the server!"
                }
            ];
            res.status(200).json({
                message: "Posts fetched successfully!",
                posts: posts
            });
            });

            module.exports = app;

有任何想法吗 ?我知道在本地下载 mongodb 可以轻松完成,但我认为在云中运行会让我学到一些东西。

此外,当尝试使用 windows powershell 进行连接时,我得到如下输出

            PS C:\Users\BHARAT> Test-NetConnection -InformationLevel detailed -ComputerName portquiz.net -Port 27017


            ComputerName            : portquiz.net
            RemoteAddress           : 52.47.209.216
            RemotePort              : 27017
            NameResolutionResults   : 52.47.209.216
            MatchingIPsecRules      :
            NetworkIsolationContext : Internet
            IsAdmin                 : False
            InterfaceAlias          : Wi-Fi
            SourceAddress           : 10.159.134.2
            NetRoute (NextHop)      : 10.159.0.1
            TcpTestSucceeded        : True
4

1 回答 1

0

我终止了集群,并且为了更好地衡量我更新了我的 IP 地址并且工作正常。

于 2019-07-09T00:17:03.943 回答