0

I'm working in a tutorial on node.js and am trying to run an app. It was working yesterday, but since adding fetch in the get request, it isn't working, I'm getting an ECONNREFUSED error. Can anyone help me fix this error? Thank you very much for your help! Donny

The final code was provided by the teacher, so I checked for errors, even pasted his code into mine, but am still getting the error

The teacher provided the final code and I even pasted it into my code, buy still getting the ECONNREFUSED error

// HERE ARE THE DEPENDENCIES for this app:
const express = require('express');
const hbs = require('express-handlebars');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');

//create an instance so we can start express
const app = express();

//add handlebars
app.engine('hbs', hbs({
  extname: 'hbs',
  defaultLayout: 'layout',
  layoutsDir: __dirname + '/views/layouts',
  partialsDir: __dirname + '/views/partials'
}));
app.set('view engine', 'hbs');

//CSS middleware
app.use("/css", express.static(__dirname + '/public/css'));
//JSON parser
const jasonParser = bodyParser.json();


//GET


app.get('/',(req,res)=>{

  fetch('http://localhost:3004/messages')
      .then(response => {
          response.json().then(json =>{
              res.render('home',{
                  articles: json
              })
          })
      })
      .catch(error => {
          console.log(error)
      })


})

app.get('/add_note', (req, res)=>{
    res.render('add_note')
})

//POST

app.post('/api/add_note', jasonParser, (req, res)=> {

  fetch('http://localhost:3004/messages',{
    method: 'POST',
    body:JSON.stringify(req.body),
    headers: {
      'Content-Type': 'application/json'
    }
  }).then((response)=>{
    res.status(200).send()
  })
})





const port = process.env.PORT || 3000;
app.listen(port,()=>{
  console.log(`Server up on port ${port}`)  
});




Error message says: 'request to http://localhost:3004/messages failed, reason: ECONNREFUSED 127.0.0.1:3004'

type: 'system', errno: 'ECONNREFUSED' code: 'ECONNREFUSED'

Donny
  • 11
  • 4
  • 1
    You didn't define `/messages` endpoint, and port 3004 is not in use (unless you defined it under `process.env.PORT`) – Nir Alfasi Jun 07 '19 at 16:16
  • 1
    I am assuming after seing this `http://localhost:3004/messages`, that you are planning to use 3rd party api. I would suggest put into a const variable and then use it. On url `http://localhost:3004/messages` seems like nothing (server, endpoint and port) exists, this is why you are getting this error. – Garry Jun 07 '19 at 16:27

1 Answers1

0

Probably just remove this line in your /etc/hosts file

::1   localhost

What could go wrong?

Deggen
  • 91
  • 1
  • 2