Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Server is in running status but when tried to hit api it is giving Error: connect ECONNREFUSED 127.0.0.1:3000 in POSTMAN

I’m trying to separate app and server. It is running properly but when tried to hit "app.get(‘/’)" api it is giving Error: connect ECONNREFUSED 127.0.0.1:3000 in Postman even after changing required settings.

In connect.js (connectDByour text)

const mongoose = require('mongoose');
const color = require('colorette');

const connectDB = (url) => {
    try {
        return mongoose.connect(url, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
    } catch (error) {
        console.log(color.bgRed(error));
    }
};
module.exports = connectDB;

In server.js

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const app = require('./app');
const http = require('http')

const port = process.env.PORT || 3000;
const connectDB = require("./database/connect");
const server = http.createServer(app);
const color = require('colorette');

// 👇️ handle uncaught exceptions
process.on('uncaughtException', function (err) {
    console.log(color.red(`Uncaught Exception Error Occurred ${err}`));
});

const startDB = async () => {
    try {
        await connectDB(process.env.MONGO_URL);
        console.log(color.blue(`DB server is connected`));
        server.listen(() => {
            console.log(color.blue(`Node server is running on port ${port}...`));
        })
    } catch (error) {
        console.log(color.red(`ERROR is ${error}`));
    }
};

startDB();

In app.js


require("dotenv").config();
const express = require("express");
const app = express();
const cors = require('cors')
const users = require("./routes/users");
app.use(cors());
app.use(express.json());

app.get('/', (req, res) => {
    res.status(200).json(`Hello World`);
})
app.use('a/user', users);
module.exports = app

On Terminal

> nodemon server.js

[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node server.js`
DB server is connected
Node server is running on port 3000...

In Postman

Error: connect ECONNREFUSED 127.0.0.1:3000
Proxy
id: "958df20d-86ee-40af-8d08-4c3274d302ca"
disabled: false
host: "127.0.0.1"
match: {…}
port: 3000
tunnel: false
authenticate: false
Request Headers
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 515bf6ca-1031-4198-8fda-9c0f99b55c83
Host: localhost:3000
Connection: keep-alive

Separation of app and server file in Express and Nodejs

>Solution :

You are trying to use express app instance from app.js as options for http.createServer(). It would not be a valid options object for createServer.

use express‘s built-in listen() method

const app = require('./app');
const http = require('http')

const port = process.env.PORT || 3000;
const connectDB = require("./database/connect");
const color = require('colorette');

// 👇️ handle uncaught exceptions
process.on('uncaughtException', function (err) {
    console.log(color.red(`Uncaught Exception Error Occurred ${err}`));
});

const startDB = async () => {
    try {
        await connectDB(process.env.MONGO_URL);
        console.log(color.blue(`DB server is connected`));
        app.listen(port, () => {
            console.log(color.blue(`Node server is running on port ${port}...`));
        })
    } catch (error) {
        console.log(color.red(`ERROR is ${error}`));
    }
};

startDB();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading