title: Postman is stuck at a "GET" request, giving error that "Could not get response Error: read ECONNRESET" in Node JS using PostgreSQL as backend
body: <p>I am following a video tutorial on login authentication using JWT Tokens.</p>
  <p>I made up a database on PostgreSQL, connected it with my Node JS server and tested out registration and login commands. Both the queries worked fine on Postman and Postman returned the expected results.</p>
  <p>Now, on logging in, I get a <strong>JWT Token</strong> in returning value, which is to be used further to get the user ID who's logged in and his information.</p>
  <p>So, to test if that token is working or not, I made a route which verifies if the user is logged in or not. (It is added in the same file in which login and registration routes are)</p>
  <p>Code of this route file file is as follows (<strong>jwAuth.js</strong>). <em>I have not added codes of Registration and Login in it</em></p>
  <pre><code>const router = require('express').Router() 
  const pool = require(&quot;../db&quot;)
  const bcrypt = require(&quot;bcryptjs&quot;);
  const jwtGenerator = require('../utils/jwtGenerator');
  const authorization = require('../middleware/authorization');
  // Registeration and Login Routes
  // Verification Route 
  router.get(&quot;/is-verify&quot;, authorization, async (res, req) =&gt; {
      try {
          res.json(true); 
          
      } catch (err) {
           console.log(err.message); 
          res.status(500).send(&quot;Server Error&quot;)
      }
  })
  module.exports = router;
  </code></pre>
  <p>The code for the middleware <strong>authorization.js</strong> is as follows,</p>
  <pre><code>const jwt = require(&quot;jsonwebtoken&quot;);
  require(&quot;dotenv&quot;).config();
  module.exports = async (req, res, next) =&gt; {
      try {
  
          const jwtToken = req.header(&quot;token&quot;);
  
          if (!jwtToken) {
              return res.status(403).json(&quot;Not Authorized&quot;);
          }
  
          // In case we do have a token, check if the token is valid or not 
          const payload = jwt.verify(jwtToken, process.env.jwtSecret);
  
          req.user = payload.user; 
  
  
      } catch (err) {
          console.log(err.message); 
          return res.status(403).json(&quot;You are not authorized&quot;);
      }
  }
  </code></pre>
  <p>I also tried the same thing on another route, dashboard.js which was supposed to return/print the <em>ID of the user using the JWT Token given to it, conditioned that the user is logged in correctly</em></p>
  <p>The code of <strong>dashboard.js</strong> is as follows,</p>
  <pre><code>const router = require('express').Router()
  const pool = require(&quot;../db&quot;)
  const authorization = require(&quot;../middleware/authorization&quot;)
  
  router.get(&quot;/&quot;, authorization, async (req, res) =&gt; {
      try {
          res.json(req.user); 
      } catch (err) {
          console.log(err.message)
          res.status(500).json(&quot;Server Error&quot;); 
      }
  })
  
  
  module.exports = router
  </code></pre>
  <p>And the code for my <strong>index.js</strong> (or server.js) is as follows,</p>
  <pre><code>const express = require('express')
  const app = express() 
  const cors = require('cors')
  
  app.use(express.json()) // Lets you use req.body 
  app.use(cors())
  
  // ROUTES 
  
  // Register and Login Routes 
  
  app.use(&quot;/auth&quot;, require(&quot;./routes/jwAuth&quot;));
  app.use(&quot;/dashboard&quot;, require(&quot;./routes/dashboard&quot;));
  
  
  app.listen(3000, () =&gt; {
      console.log(&quot;Console is running&quot;);
  })
  </code></pre>
  <p>My problem is, If I try to send request to the following links with the required data, Postman works fine and data is also added in my database (and is also retrieved correctly while checking for login)</p>
  <pre><code>http://localhost:3000/auth/register
  http://localhost:3000/auth/login
  </code></pre>
  <p>But when I try the same thing with this URL (by giving the JWT Token in the Header of the Postman <strong>GET</strong> Request,</p>
  <pre><code>http://localhost:3000/dashboard
  http://localhost:3000/auth/is-verify
  </code></pre>
  <p>Postman gets stuck, takes a long time in processing the request and ultimately gives this error,</p>
  <p><strong>Could not get response
  Error: read ECONNRESET</strong></p>
  <p>I tried searching this error up and apparently the fix of this issue is adding an IP address of localhost in <strong>etc/hosts</strong> file but in that case, my registration and login <strong>POST Requests</strong> would not have worked either. I am unable to figure out the problem so kindly someone please help.</p>
  <p>Just to be sure, I have shared below the etc/hosts file as well and the Registration/Login Routes along with jwtGenerator file</p>
  <p><strong>etc/hosts file</strong></p>
  <pre><code># Copyright (c) 1993-2009 Microsoft Corp.
  #
  # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
  #
  # This file contains the mappings of IP addresses to host names. Each
  # entry should be kept on an individual line. The IP address should
  # be placed in the first column followed by the corresponding host name.
  # The IP address and the host name should be separated by at least one
  # space.
  #
  # Additionally, comments (such as these) may be inserted on individual
  # lines or following the machine name denoted by a '#' symbol.
  #
  # For example:
  #
  #      102.54.94.97     rhino.acme.com          # source server
  #       38.25.63.10     x.acme.com              # x client host
  
  # localhost name resolution is handled within DNS itself.
  #   127.0.0.1       localhost
  #   ::1             localhost
  
  0.0.0.0 hss.hsselite.com
  0.0.0.0 www.hss.hsselite.com
  0.0.0.0 d1v9mrqde8r3oj.cloudfront.net
  0.0.0.0 www.d1v9mrqde8r3oj.cloudfront.net
  0.0.0.0 api.hsselite.com
  0.0.0.0 www.api.hsselite.com
  0.0.0.0 hsselite.com/trial/step2.php
  0.0.0.0 www.hsselite.com/trial/step2.php
  0.0.0.0 anchorfree.com
  0.0.0.0 www.anchorfree.com
  0.0.0.0 box.anchorfree.net
  0.0.0.0 www.box.anchorfree.net
  0.0.0.0 rpt.anchorfree.net
  0.0.0.0 www.rpt.anchorfree.net
  0.0.0.0 123.box.anchorfree.net
  0.0.0.0 www.123.box.anchorfree.net
  0.0.0.0 anchorfree.us
  0.0.0.0 www.anchorfree.us
  0.0.0.0 delivery.anchorfree.us/land.php
  0.0.0.0 www.delivery.anchorfree.us/land.php
  0.0.0.0 rss2search.com
  0.0.0.0 www.rss2search.com
  0.0.0.0 mefeedia.com
  0.0.0.0 www.mefeedia.com
  0.0.0.0 a433.com
  0.0.0.0 www.a433.com
  0.0.0.0 techbrowsing.com
  0.0.0.0 www.techbrowsing.com
  0.0.0.0 techbrowsing.com/away.php
  0.0.0.0 www.techbrowsing.com/away.php
  0.0.0.0 update.mydati.com
  0.0.0.0 www.update.mydati.com
  0.0.0.0 mydati.com
  0.0.0.0 www.mydati.com
  0.0.0.0 geo.mydati.com 
  0.0.0.0 www.geo.mydati.com 
  0.0.0.0 updateeu.mydati.com
  0.0.0.0 www.updateeu.mydati.com
  0.0.0.0 east.mydati.com
  0.0.0.0 www.east.mydati.com
  0.0.0.0 west.mydati.com
  0.0.0.0 www.west.mydati.com
  0.0.0.0 us.mydati.com
  0.0.0.0 www.us.mydati.com
  0.0.0.0 eu.mydati.com
  0.0.0.0 www.eu.mydati.com
  0.0.0.0 myd3.mydati.com
  0.0.0.0 www.myd3.mydati.com
  0.0.0.0 ns2.mydati.com
  0.0.0.0 www.ns2.mydati.com
  0.0.0.0 ns1.mydati.com
  0.0.0.0 www.ns1.mydati.com
  </code></pre>
  <p><strong>jwtGenerator.js</strong> File</p>
  <pre><code>const jwt = require('jsonwebtoken');
  require('dotenv').config(); 
  
  function jwtGenerator(user_id) {
      const payload = {
          user: user_id
      }
      return jwt.sign(payload, process.env.jwtSecret, {expiresIn: &quot;1h&quot;})
  
  }
  
  module.exports = jwtGenerator; 
  </code></pre>
  <p><strong>jwAuth.js Complete Code</strong></p>
  <pre><code>const router = require('express').Router() 
  const pool = require(&quot;../db&quot;)
  const bcrypt = require(&quot;bcryptjs&quot;);
  const jwtGenerator = require('../utils/jwtGenerator');
  const authorization = require('../middleware/authorization');
  // Registeration 
  
  router.post(&quot;/register&quot;, async (req, res) =&gt; {
      try {
  
          // 1. Destructure the req.body (name, email, password) 
  
          const {name, email, password} = req.body; 
  
          // 2. Check if the user exists (If user already exists, then throw error)
  
          const user = await pool.query(&quot;SELECT * FROM users WHERE user_email = $1&quot;, [email]);
  
          if (user.rows.length !== 0)
          {
              return res.status(401).send(&quot;User Already Exists&quot;);
          }
  
          // 3. Bcrypt the user password 
  
          const saltRound = 10; 
          const salt = await bcrypt.genSalt(saltRound); 
          const bcryptPassword = await bcrypt.hash(password, salt); 
  
          // 4. Enter the new user inside our database 
  
          const newUser = await pool.query(&quot;INSERT INTO users (user_name, user_email, user_password) values ($1, $2, $3) RETURNING *&quot;, [name, email, bcryptPassword]);
  
          // 5. Generating Our JWT Token 
  
          const token = jwtGenerator(newUser.rows[0].user_id);
  
          res.json({token}); 
  
      } catch (err) {
          console.log(err.message); 
          res.status(500).send(`Server Error ${err}`); 
      }
  })
  
  // Login Route 
  
  router.post(&quot;/login&quot;, async (req, res) =&gt; {
      try {
  
          // 1. Destructure the req.body 
  
          const {email, password} = req.body; 
  
          // 2. Check if user does not exist (Throw error if user does not exist)
  
          const user = await pool.query(&quot;Select * from users where user_email = $1&quot;, [email]);
  
          if (user.rows.length === 0) {
              res.status(401).send(&quot;Email does not Exist&quot;); 
          }
  
          // 3. Check if the password is same for the user in database 
  
          const validPassword = await bcrypt.compare(password, user.rows[0].user_password);
          if (!validPassword) {
              return res.status(401).send(&quot;Password is incorrect&quot;); 
          }
  
          // 4. Give them a JWT Token 
  
          const token = jwtGenerator(user.rows[0].user_id); 
  
          res.json({token});
  
  
      } catch (err) {
          console.log(err.message); 
          res.status(500).send(&quot;Server Error&quot;)
      }
  });
  
  router.get(&quot;/is-verify&quot;, authorization, async (res, req) =&gt; {
      try {
          res.json(true); 
          
      } catch (err) {
           console.log(err.message); 
          res.status(500).send(&quot;Server Error&quot;)
      }
  })
  
  module.exports = router;
  </code></pre>