I am getting Error: read ECONNRESET

My question: Why i am getting this issue?

Details (like screenshots):

image

How I found the problem:

I have also written my router.js file code for clear understanding.

const express = require('express');

const router = express.Router();

// GET ALL API
router.get('/getAll', (req , res) => {
    res.send('Gettting All The Data')
})

// GET By ID API
router.get('/getOne/:id', (req , res) => {
    res.send("ID : ${req.params.id}")
})

// POST API
router.post('/post', (req , res) => {
    res.send('POST Method')
})

// PUT API
router.put('/put', (req , res) => {
    res.send('PUT METHOD')
})

//DELETE API
router.delete('/delete', (req , res) => {
    res.send('DELETE Method')
})


module.exports = router;

I’ve already tried:

  • ALready tried all the method available online

This happens when the server closes the TCP/IP connection before the client receives a response.

Where in your code do you specify the /api/ path prefix on your endpoints? Your screenshot of the console error indicates localhost/api/put for example. Is there more code below the Node/Express code that you’ve shared that adds that as a path prefix? Can you also include that please?

No haven’t added more code till now. I only have 2 files containing code index.js and routes.js. I have already shared routes.js code and wait I will share index.js code also

require('dotenv').config()

const express = require('express')
const mongoose = require('mongoose')
const mongoData = process.env.DATABASE_URL;
// Let's connect to our MongoDB Atlas Database
mongoose.connect(mongoData , {useNewUrlParser: true})
const database = mongoose.connection;

database.on('error' , (error) => {
    console.log(error)
})

database.once('connected' , () =>{
    console.log('Database Connected')
})

// This will be the app that we will be using for the routing of our different endpoints
const app = express();

app.use(express.json)

const routers = require('./routes/routes')

// So all our end points will start from the address /api
app.use('/api' , routers)

app.listen(4000, () => {
    console.log('Server Started at ${4000}')
})

The code above written is of index.js which is my project main file. But still after all try I am not able to get anything out of it.

Thanks for sending over that other code. I’d suggest putting more console logging in your source to trace what’s happening, or look at some debuggers to help from there.

I truly hate to be the “it works on my machine” guy, but I put together a quick node/express app based on what you sent, and I can access localhost:4000 without getting that error (running Postman desktop app on Windows 11, accessing localhost:4000 on a WSL2 instance running the Node app)

I wondered if it was a CORS issue, but that didn’t matter in my example code. Without more debugging on your side, I can’t really diagnose further what the problem is.