连接 ECONNREFUSED 127.0.0.1:27017

发布于 2025-01-10 14:54:15 字数 2811 浏览 0 评论 0原文

我需要帮助来连接我的 Express 应用程序和 MongoDB。 我相信我写的代码很完美,但我还不知道如何连接到 MongoDB。哦,我是 MongoDB 和 Atlas 的新手。 当我用谷歌搜索时,我需要添加一个IP地址,我想在其中使用Atlas上的数据库。我就是这样做的,如下所示。 MongoDB Atlas 我不知道为什么在127.0.0.1的末尾添加了/32。我尝试添加“localhost”,但出现错误。我在谷歌上找到了很多解决方案,例如写 127.0.0.1 而不是 localhost。我尝试了一切,但仍然不起作用。请帮我解决这个错误。预先非常感谢。

这是我的代码。我认为没有问题。

const express = require("express")
const mongoose = require('mongoose')
const articleRouter = require('./routes/articles')
const app = express()

mongoose.connect('mongodb://localhost/blog')
mongoose.connect('mongodb://localhost/blog', {
  useNewUrlParser: true, useUnifiedTopology: true
})

// 2
// mongoose.connect('mongodb://localhost/blog',
//   err => {
//     if(err) throw err;
//     console.log('connected to MongoDB')
// })

// 3
// mongoose.connect('mongodb://0.0.0.0:27017/',
//   err => {
//     if(err) throw err;
//     console.log('connected to MongoDB')
// })

// 4
// var MongoClient = require('mongodb').MongoClient;
// MongoClient.connect("mongodb://localhost:27017/MyDb", function (err, db) {
//   if(err) throw err;
// });

// 5
// const mongoose = require("mongoose");
// mongoose.connect("mongodb://127.0.0.1:27017/blog")
// .then(() => {
//   console.log("Connected to Database");
// })
// .catch((err) => {
//   console.log("Not Connected to Database ERROR! ", err);
// });

app.set('view engine', 'ejs')

app.use(express.urlencoded({ extended: false }))

app.get('/', (req, res) => {
  const articles = [{
    title: "Test Article",
    createdAt: new Date(),
    description: "test description"
  }, {
    title: "Test Article2",
    createdAt: new Date(),
    description: "test description"
  }]

  res.render('articles/index', { articles: articles })
})

app.use('/articles', articleRouter)
app.listen(5000)

const express = require('express')
const Article = require('./../models/article')
const router = express.Router()

router.get('/new', (req, res) => {
  res.render('articles/new', { article: new Article() })
})
router.get('/:id', async (req, res) => {
  const article = await Article.findById(req.params.id)
  if (article === null) res.redirect('/')
  res.render('articles/show', { article: article })
})

router.post('/', async (req, res) => {
  let article = new Article({
    title: req.body.title,
    description: req.body.description,
    markdown: req.body.markdown,
  })
  try {
    article = await article.save()
    res.redirect(`/articles/${article.id}`)
  } catch (e) {
    console.log(e)
    res.render('articles/new', { article: article })
  }
})


module.exports = router

I need help to connect my express app and MongoDB.
I believe I wrote code perfectly, but I haven't been known how to connect to MongoDB. Oh, I am new to MongoDB and Atlas.
As I googled, I need to add an IP address in which I wanna use db on Atlas. I did so like below.
MongoDB Atlas
I don't know why /32 was added at the end of 127.0.0.1. I tried to add "localhost", but got an error. I found so many solutions on googled such as writing 127.0.0.1 instead of localhost. I tried everything but it still doesn't work. Please help me with the error. Thanks so much in advance.

Here are my code. I don't think there is an issue.

const express = require("express")
const mongoose = require('mongoose')
const articleRouter = require('./routes/articles')
const app = express()

mongoose.connect('mongodb://localhost/blog')
mongoose.connect('mongodb://localhost/blog', {
  useNewUrlParser: true, useUnifiedTopology: true
})

// 2
// mongoose.connect('mongodb://localhost/blog',
//   err => {
//     if(err) throw err;
//     console.log('connected to MongoDB')
// })

// 3
// mongoose.connect('mongodb://0.0.0.0:27017/',
//   err => {
//     if(err) throw err;
//     console.log('connected to MongoDB')
// })

// 4
// var MongoClient = require('mongodb').MongoClient;
// MongoClient.connect("mongodb://localhost:27017/MyDb", function (err, db) {
//   if(err) throw err;
// });

// 5
// const mongoose = require("mongoose");
// mongoose.connect("mongodb://127.0.0.1:27017/blog")
// .then(() => {
//   console.log("Connected to Database");
// })
// .catch((err) => {
//   console.log("Not Connected to Database ERROR! ", err);
// });

app.set('view engine', 'ejs')

app.use(express.urlencoded({ extended: false }))

app.get('/', (req, res) => {
  const articles = [{
    title: "Test Article",
    createdAt: new Date(),
    description: "test description"
  }, {
    title: "Test Article2",
    createdAt: new Date(),
    description: "test description"
  }]

  res.render('articles/index', { articles: articles })
})

app.use('/articles', articleRouter)
app.listen(5000)

const express = require('express')
const Article = require('./../models/article')
const router = express.Router()

router.get('/new', (req, res) => {
  res.render('articles/new', { article: new Article() })
})
router.get('/:id', async (req, res) => {
  const article = await Article.findById(req.params.id)
  if (article === null) res.redirect('/')
  res.render('articles/show', { article: article })
})

router.post('/', async (req, res) => {
  let article = new Article({
    title: req.body.title,
    description: req.body.description,
    markdown: req.body.markdown,
  })
  try {
    article = await article.save()
    res.redirect(`/articles/${article.id}`)
  } catch (e) {
    console.log(e)
    res.render('articles/new', { article: article })
  }
})


module.exports = router

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文