0

I've started to build a typescript library (intended to be used on the server side) and right now I'm trying to use the node repl to play around with my code and see what happens in certain situations... I've built and required the file, but now I'm having a problem: I have a function that takes a http Request (type Request from express.js), and I'd like to try and run it in the repl providing it with a copy of a request that I previously made from my browser. Is this feasible? I thought maybe I could do it by either:

  • doing regex magic on the request exported as cURL or
  • sending the request to node, but then how am I going to receive it while in the repl?
antzshrek
  • 9,276
  • 5
  • 26
  • 43
iotus
  • 45
  • 5
  • can you please provide an example to the code you're talking about. – antzshrek Jun 02 '20 at 14:07
  • in chrome you can export one or all the requests to various code, curl, fetch etc which contain all the headers and body (right click any request in network tab and go into the sub-menu Copy and it has a list) – Lawrence Cherone Jun 02 '20 at 14:34

2 Answers2

-1

I'm not sure I understand your use-case, but you can try something like this:

In some temp folder type:

npm install "request-promise"

Then from the same temp folder, enter the REPL and type:

(async () => {const response = await require("request-promise").get("https://cnn.com"); console.log(response)})()

This example is for get, but it can be easily changed to other HTTP methods.

Perspectivus
  • 972
  • 1
  • 9
  • 22
-1

I've found a fairly simple way to do what I want... It involved quickly setting up a basic express server (set up following this tutorial):

mkdir scratch && cd scratch && npm init

(select defaults except entrypoint app.js)

npm i express

Create an app.js (vi app.js) with the following contents:

var express = require('express');
var app = express();
var circ = {};
circ.circ = circ;
var cache = [];
app.get('/', function (req, res) {
  res.send(JSON.stringify(req, (key, value) => {
  if (typeof value === 'object' && value !== null) {
    // Duplicate reference found, discard key
    if (cache.includes(value)) return;

    // Store value in our collection
    cache.push(value);
  }
  return value;
}));
});
app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

(See this answer for JSON.stringify custom replacer resp. second argument to JSON.stringify). You can optionally use flatted instead, which I discovered later and is surely better.

Now do the following:

  1. Run the app with node app.js
  2. In your browser, navigate to the website where your desired request is posted to.
  3. Open your browsers development tools (Ctrl+shift+c works for me).
  4. Go to the network tab.
  5. Find the request that interests you and right click on it.
  6. Click on copy > copy as curl (or similar, depending on which browser you're using).
  7. Run that curl request, but change the url it's posted to to 127.0.0.1:3000 (e.g. change curl 'example.com' \...etc to curl '127.0.0.1:3000' \...etc

You should now get that request on standard output as a JSON object and it's in the format that express usually deals with. Yay! Now, pipe it into your clipboard (likely xclip -selection c on linux) or probably even better, redirect it to a file.

...

Step 2 - ? Step 3 - Profit :)

RobC
  • 22,977
  • 20
  • 73
  • 80
iotus
  • 45
  • 5