服务器上的MongoDB net :: ERR_CONNECTION_REFUSED

时间:2019-02-24 19:13:54

标签: node.js mongodb mongoose

我正在尝试从服务器上托管的react / node应用程序对数据库进行调用。当在本地运行(使用nodemon)时,应用程序运行在localhost:3000上,并且服务器托管在端口4000上,因此我对localhost:4000进行了api调用。例如,localhost:4000/students显示MongoDB中存储的学生列表。

我的server.js如下:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = 4000;
const cors = require('cors');
const mongoose = require('mongoose');
const studentRoute = require('./student.route');
const config = require('./secrets');

mongoose.Promise = global.Promise;
mongoose.connect(config.dbUri, { useNewUrlParser: true }).then(
  () => {console.log('Database is connected') },
  err => { console.log('Can not connect to the database'+ err)}
);

我通过将npm run build生成的构建文件夹上载到名为“ the-wall”(项目名称)的文件夹来将我的应用程序部署到我的站点。因此,要访问该应用程序,请访问example.com/the-wall。

我的mongoDB使用MongoDB地图集托管,并且已将我的IP地址和我认为是服务器的IP地址列入了白名单。我正在使用axios进行呼叫,并具有以下配置:


const env = process.env.NODE_ENV; 

export const app = axios.create({
  baseURL:
    env === 'production'
      ? 'https://example.com:4000/'
      : 'http://localhost:4000/',
});

但是,当我尝试访问example.com:4000/student时,出现了net::ERR_CONNECTION_REFUSED错误。据我所知,服务器上未安装mongoDB。我使用的网址不正确,还是我需要做其他设置?

注意:我也尝试过example.com/the-wall:4000/student,但是我遇到了404错误。

我尝试拨打数据库的方式看起来正确吗? (即,URL是否与端口正确等)

1 个答案:

答案 0 :(得分:1)

尝试使其在生产数据库中本地运行。

如果有效,则是服务器上的IP配置问题。

如果失败,则是代码配置问题。