webservice的响应不起作用

时间:2018-01-18 10:54:29

标签: javascript node.js reactjs express axios

我正在使用express和NodeJS。我创建了一个helloworld webservice。 我在ReactJS中使用Axios调用此Web服务。 但我对Webservice的响应有疑问 这是我的网络服务:

var busboy = require('connect-busboy');
const app = express()
const port = 4002
app.use(bodyParser())
app.use(busboy());
app.use('/', express.static('./'));   
app.get('/helloworld', (req, res) => {
   return res.send('hello world')
})

在这里,我打电话给网络服务:

callWs = () => {
        axios.get(`http://localhost:4002/helloworld`)
        .then(response => {            
           console.log('WS') // it doesn't work
        }) .catch(function (error) {
            if (error.response) {
                console.log('Error data : ', error.response.data);
                console.log('Error status : ', error.response.status);
                console.log('Error headers : ', error.response.headers);
            } else if (error.request) {
                console.log('Error request : ', error.request);
            } else {
                console.log('Error message : ', error.message);
            }
            console.log(error.config);
        })                 
    }

在这里,导航器的控制台: enter image description here

它只适用于json数据

你知道吗? 谢谢

2 个答案:

答案 0 :(得分:0)

使用您显示的代码,我发现您缺少服务器中的端口设置:

{% set regiones = {
    patagonia: {
        title: "Patagonia",
        cities: {
            neuquen:        { title: "Neuquén"},
            rionegro:       { title: "Río Negro"},
            chubut:         { title: "Chubut"},
            santacruz:      { title: "Santa Cruz"},
            tierradelfuego: { title: "Tierra del Fuego"},
        },
    },
    pampa: {
        title: "Pampa",
        cities: {
            buenosaires: { title: "Buenos Aires"},
            cordoba:     { title: "Córdoba"},
            lapampa:     { title: "La Pampa"},
            santafe:     { title: "Santa Fe"},
        },
    },
    cuyo: {
        title: "Cuyo",
        cities: {
            mendoza: { title: "Mendoza"},
            sanjuan: { title: "San Juan"},
            sanluis: { title: "San Luis"},
        },
    },
    noreste: {
        title: "Noreste",
    },
    noroeste: {
        title: "Noroeste",
    },
} %}

<nav>
   <ul>
      {% for slug, item in regiones %}
         <li>
             <a href="/{{ slug }}">{{ item.title }}</a>
             {% if item.cities is defined %}
                 <ul>
                     {% for slugg, itemm in item.cities %}
                         <li><a href="/{{ slugg }}">{{ itemm.title }}</a></li>
                     {% endfor %}
                 </ul>
             {% endif %}
         </li>
      {% endfor %}
   </ul>
</nav>

没有它我会收到一个ECONNREFUSED错误。

如果添加并将函数中的行更改为:

app.listen(port);

您已正确收到callWs = () => { axios.get(`http://localhost:4002/helloworld`) .then(response => { console.log(response.data) }) .catch(function (error) { if (error.response) { console.log('Error data : ', error.response.data); console.log('Error status : ', error.response.status); console.log('Error headers : ', error.response.headers); } else if (error.request) { console.log('Error request : ', error.request); } else { console.log('Error message : ', error.message); } console.log(error.config); }) } 消息。

答案 1 :(得分:0)

1-您应该在最后添加此行来启动快速服务器:

app.listen(port, () => console.log(`Express app listening on port ${port}!`))

2-您正在从根目录服务static files,您必须创建一个public文件夹并使用它来呈现静态文件:

const path = require("path");
...
app.use(express.static(path.join(__dirname, 'public')));

3-如果您想管理文件上传,请使用multer包而不是busboy

最后,您的服务器端代码应如下所示:

const express = require('express'),
    app = express(),
    port = 4002,
    bodyParser = require('body-parser'),
    path = require("path");

// body parser middleware
app.use(bodyParser());
// serving static files from ./public folder
app.use(express.static(path.join(__dirname, 'public')));

app.get('/helloworld', (req, res) => {
    return res.send('hello world')
});

app.listen(port, () => console.log(`Express app listening on port ${port}!`))