【问题标题】:Response of webservice doesn't workWeb服务的响应不起作用
【发布时间】:2018-01-18 10:54:28
【问题描述】:

我正在使用 express 和 NodeJS。我创建了一个 helloworld 网络服务。 我用 Axios 在 ReactJS 中调用这个 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')
})

这里,我调用了webservice:

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

这里是 navigator 的控制台:

它只适用于 json 数据

你有什么想法吗? 谢谢

【问题讨论】:

    标签: javascript node.js reactjs express axios


    【解决方案1】:

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

    app.listen(port);
    

    没有它,我会收到 ECONNREFUSED 错误。

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

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

    您正确地收到了hello world 消息。

    【讨论】:

    • 谢谢,但这是一回事:(
    【解决方案2】:

    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}!`))
    

    【讨论】:

    • 感谢您的回答,但我已经测试了您的解决方案,而且是同样的事情.. 调用了 web 服务,但我在响应中遇到了同样的错误
    • 例如,当您使用`node app.js`启动Web服务时,它会在控制台中显示一些内容吗?比如错误或日志之类的东西
    • 当我用邮递员调用我的网络服务时,这很好。我相信问题在前面
    • 你配置了react代理吗,见configure proxy部分
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多