2

I am stuck in a demo app where i have added override fetch function of node-fetch npm package. Like this:

import Hook from "require-in-the-middle";

Hook(["node-fetch"], function (exported) {
  const wrappedFetch = wrappedNodeFetch(exported);
  exported = wrappedFetch;
  return exported;
});

And trying a async post client request in a POST handler of Node.js server. But upon simulation using axios client to the POST handler declared, it is throwing an error "ERR_NON_2XX_3XX_RESPONSE".

require("typescript-sdk/dist/integrations/express/register"); // Require the express hook
require("typescript-sdk/dist/integrations/node-fetch/require") // Require the node-fetch hook
var express = require('express');
const fetch = require("node-fetch")

var app = express();    
app.use(express.json());

app.post('/postData', async function (re, rs) {
    console.log("POST request at /postData route");
    console.log("---",re.body, typeof re.body);

    fetch('https://reqres.in/api/users/2')
    .then(res => res.text())
    .then(text2 => {
        console.log("send text from server", text2)
        rs.send(text2)
    })

    console.log("post handler finished")
});

var server = app.listen(3010,() =>
console.log(`Example app listening on port 3010!`));'

simulating by axios like this

const r = await axios({
         method: "POST",
         baseURL: `http://localhost:3010/postData`,
         data: '{"name":"Chandler","job":"Data Processor"}',
       });

with Headers:

            accept: '*/*'
            accept-encoding: gzip, deflate, br
            connection: close
            content-length: "46"
            content-type: application/json;charset=UTF-8
            host: localhost:8080
            user-agent: Thunder Client (https://www.thunderclient.com)
Ritik Jain
  • 89
  • 1
  • 6
  • Any error thrown at the server? Any output at all at the server? And j suppose the missing `"` after `"POST` is just a typo here ... – derpirscher Oct 11 '22 at 06:55
  • No there is no error at server level here. – Ritik Jain Oct 11 '22 at 08:14
  • Any output at all? At least `"POST request at /postData route"` should be printed, when the route is hit. If not even that is shown, you probably get a 404. You could of course catch the error on the client side and further inspect the response to see WHICH status code is returned ... – derpirscher Oct 11 '22 at 09:24
  • I have run in debugger here. The post handler is executing but finish event is not triggering. And then also same problem is happening – Ritik Jain Oct 12 '22 at 06:18

0 Answers0