Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send a request at JSON format using http module to an ExpressJS server? #5517

Closed
AlirezaEthDev opened this issue Mar 3, 2024 · 1 comment
Labels

Comments

@AlirezaEthDev
Copy link

Two web servers are in place: one is developed using the Express.js framework to manage requests and responses, while the other is built with the HTTP module to forward requests to the former. The current requirement is to send requests in JSON format, but there is uncertainty on how to achieve this using the HTTP module.

The first server by express js:

const { Web3 } = require("web3");
const {toChecksumAddress}=require("ethereum-checksum-address");
const express=require("express");
const bodyParser=require("body-parser");

const walletMap=new Map();

const urlencodedParser=new bodyParser.urlencoded({extended:false});
const app=express();
app.use(bodyParser.json());

...

app.get("/wallet-status",function(req,res){
  try{
    const response=walletView(req.body.address);
    res.status(200).json(response);
  }catch(err){
    const errorObj={
      error:err.name,
      message:err.message
    }
    res.status(400).json(errorObj);
  }
})

...

function walletView(address){
    address=address.toString();
    if(toChecksumAddress(address)){
      if(walletMap.get(address)===undefined){
        return {"message":"The address provided has not been recorded previously"}
      }else{
        return walletMap.get(address);
      }
    }
}

The second server by http module:

const http=require("http");

let options={
    host:"127.0.0.1",
    port:2020,
    path:"/wallet-status",
    method:"GET",
    headers:{
        "Content-Type":"application/json"
    }
}

const httpRequest=http.request(options,function(response){
    console.info(response.statusCode);
})

httpRequest.end();

What modifications are needed for the second app to send JSON requests, allowing the callback function of app.get("/wallet-status",...) to receive them?

@wesleytodd
Copy link
Member

Hey @AlirezaEthDev, we do not to technical support here. Please use stackoverflow or reddit to ask general questions like this. This is for bugs, features, and otherwise improving the express project itself. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants