如何修复Node.js应用程序中的ECONNREFUSED错误

时间:2019-06-07 16:11:41

标签: node.js resolve econnrefused

我正在研究有关node.js的教程,并试图运行一个应用程序。昨天它在工作,但是由于在获取请求中添加了提取,所以它不工作,我收到了ECONNREFUSED错误。谁能帮我解决此错误?非常感谢您的帮助!唐尼

最终的代码是由老师提供的,所以我检查了错误,甚至将他的代码粘贴到了我的代码中,但是仍然出现错误

老师提供了最终的代码,我什至将其粘贴到了我的代码中,购买仍然得到ECONNREFUSED错误

// 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}`)  
});




错误消息显示: '对http://localhost:3004/messages的请求失败,原因: ECONNREFUSED 127.0.0.1:3004'

类型:“系统”, errno:“ ECONNREFUSED” 代码:“ ECONNREFUSED”

0 个答案:

没有答案