From ffe7820974ef2ada9a67c0b6d4d06e5a2cb9b709 Mon Sep 17 00:00:00 2001 From: Muhammad Naeem Date: Tue, 22 Nov 2022 04:37:55 +0500 Subject: [PATCH] Fixed connect ECONNREFUSED ::1:3000 Fixed error: "message":"connect ECONNREFUSED ::1:3000","name":"Error","stack":"Error: connect ECONNREFUSED ::1:3000\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)","config":{"url":"http://localhost:3000/api/users","method":"get","headers":{"Accept":"application/json, text/plain, */*","User-Agent":"axios/0.21.0"},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1},"code":"ECONNREFUSED" render.js and index.js had PORT hard cored in a code. The PORT entered in config.env rather than 3000 show error. --- assets/js/index.js | 6 +++--- server/services/render.js | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 2af5424..a9ab17e 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -1,4 +1,4 @@ - +const PORT = process.env.PORT; $("#add_user").submit(function(event){ @@ -17,7 +17,7 @@ $("#update_user").submit(function(event){ var request = { - "url" : `http://localhost:3000/api/users/${data.id}`, + "url" : `http://localhost:${PORT}/api/users/${data.id}`, "method" : "PUT", "data" : data } @@ -34,7 +34,7 @@ if(window.location.pathname == "/"){ var id = $(this).attr("data-id") var request = { - "url" : `http://localhost:3000/api/users/${id}`, + "url" : `http://localhost:${PORT}/api/users/${id}`, "method" : "DELETE" } diff --git a/server/services/render.js b/server/services/render.js index 22068b7..d5befe0 100644 --- a/server/services/render.js +++ b/server/services/render.js @@ -1,9 +1,10 @@ const axios = require('axios'); +const PORT = process.env.PORT; exports.homeRoutes = (req, res) => { // Make a get request to /api/users - axios.get('http://localhost:3000/api/users') + axios.get(`http://localhost:${PORT}/api/users`) .then(function(response){ res.render('index', { users : response.data }); }) @@ -19,7 +20,7 @@ exports.add_user = (req, res) =>{ } exports.update_user = (req, res) =>{ - axios.get('http://localhost:3000/api/users', { params : { id : req.query.id }}) + axios.get(`http://localhost:${PORT}/api/users`, { params : { id : req.query.id }}) .then(function(userdata){ res.render("update_user", { user : userdata.data}) })