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)