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'