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.