AngularJS + ExpressJS. Proxy POST request is pending

后端 未结 1 637
长发绾君心
长发绾君心 2020-12-10 06:04

Using AngularJS + Express I have the following code to proxy my requests to a remote service:

app.get(\'/api.json\', function (req, res) {
    req.pipe(reque         


        
相关标签:
1条回答
  • 2020-12-10 06:31

    You should have mentioned you were using the request library:

    https://github.com/mikeal/request

    request.post() is expecting the form either as the second parameter:

    request.post('http://service.com/upload', {form:{key:'value'}})
    

    or as a chained call:

    request.post('http://service.com/upload').form({key:'value'})
    

    Because you're not passing it as an argument, request.form() is not making any request at all, waiting for you to call .form(). But since you're not doing that either, no request ever happens, so no answer is ever returned, and thus your application sees that the request failed without response. You can see that in the chrome developer tools network tab, where the request will show a "(failed)" status code.

    So just obtain the form data from the current request and pass it to request.form and it should work.

    For future reference, a debugger would have told you what the mistake was instantly. I recommend the one included with Webstorm, but feel free to use any debugger at all.

    Edit: Haven't tried but this is what I would try

    app.post('/api.json', function (req, res) {
        req.pipe(request.post("http://test-api.com/api.json", {form:req.body})).pipe(res);
    });
    
    0 讨论(0)
提交回复
热议问题