邮递员:连接 ECONNREFUSED 127.0.0.1:5005

Postman: Connect ECONNREFUSED 127.0.0.1:5005

我开发了一个 API 端点。之前工作正常。不幸的是,项目文件夹已损坏(我重新创建了文件 db.js 和 server.js)。但是现在当我尝试从 API 获取数据时,我得到:

"connect ECONNREFUSED 127.0.0.1:5005"

我使用的 URL 是 localhost:

我的服务器 运行 在同一端口上,即 5005:

有人可以详细说明可能是什么问题吗?我的直觉是,当我重新创建文件时,我可能遗漏了一些东西:

db.js:

const mongoose = require('mongoose');

const userName = "myUsername"
const password = "myPassword"
const dbName = "comfyRooms"

const dbURL = `mongodb+srv://${userName}:${password}@mongo-cluster.damzf.mongodb.net/${dbName}?authSource=admin&replicaSet=atlas-s7z01e-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true`

mongoose.connect(dbURL, {useUnifiedTopology: true, useNewUrlParser: true})
let connection = mongoose.connection

connection.on('error', () => {
    console.log('Unable to connect to MongoDB')
})

connection.on('connected', () => {
    console.log("MongoDB connection established :)")
})

module.exports = mongoose

server.js

const express = require('express')
const app = express()

const dbConfig = require('./db')
const roomsRoute = require('./routes/roomsRoute')

app.use('/api/rooms', roomsRoute)

const port = process.env.PORT || 5005

app.listen(() => {
    console.log("Node JS server listening on port " + port)
})

roomsRoute.js:

const express = require('express');
const router = express.Router();

const Room = require('../models/rooms');

router.get('/getallrooms', async (req, res) => {
    try {
        const rooms = await Room.find({});
        return res.send(rooms);
    } catch (error) {
        return res.status(400).json({message: error});
    }
});

module.exports = router;

我已附上重要文件。如果缺少任何其他信息,请告诉我。谢谢!

您没有将端口变量传递给监听函数,您只是在记录它

app.listen(() => {
console.log("Node JS server listening on port " + port)
})

这应该有效

app.listen(port, () => {
  console.log("Node JS server listening on port " + port)
})