简体   繁体   English

如何在节点repl中获取请求

[英]how to get a request in node repl

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.我已经开始构建一个 typescript 库(打算在服务器端使用),现在我正在尝试使用节点 repl 来玩弄我的代码,看看在某些情况下会发生什么......我已经构建并需要该文件,但现在我遇到了一个问题:我有一个 function 需要一个 http 请求(来自 express.js 的请求类型),我想尝试在 repl 中运行它,为它提供一个我之前从浏览器发出的请求的副本。 Is this feasible?这可行吗? I thought maybe I could do it by either:我想也许我可以通过以下方式做到这一点:

  • doing regex magic on the request exported as cURL or对导出为 cURL 或
  • sending the request to node, but then how am I going to receive it while in the repl?将请求发送到节点,但是在 repl 中我将如何接收它?

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:然后从同一个临时文件夹中,输入 REPL 并键入:

(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.此示例用于get ,但可以轻松更改为其他 HTTP 方法。

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) (选择除入口点 app.js 之外的默认值)

npm i express

Create an app.js ( vi app.js ) with the following contents:创建一个包含以下内容的 app.js ( vi app.js ):

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 ). (请参阅JSON.stringify自定义JSON.stringifyJSON.stringify的第二个参数的答案)。 You can optionally use flatted instead, which I discovered later and is surely better.您可以选择使用flatted代替,这是我后来发现的,而且肯定更好。

Now do the following:现在执行以下操作:

  1. Run the app with node app.js使用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).打开您的浏览器开发工具( Ctrl + shift + c对我有用)。
  4. Go to the network tab. Go 到网络选项卡。
  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).单击复制 > 复制为 curl(或类似名称,具体取决于您使用的浏览器)。
  7. Run that curl request, but change the url it's posted to to 127.0.0.1:3000 (eg change curl 'example.com' \...etc to curl '127.0.0.1:3000' \...etc Run that curl request, but change the url it's posted to to 127.0.0.1:3000 (eg 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.您现在应该在标准 output 作为 JSON object 上获得该请求,并且它采用 express 通常处理的格式。 Yay, Now, pipe it into your clipboard (likely xclip -selection c on linux) or probably even better, redirect it to a file.是的,现在,pipe 将其放入剪贴板(可能是xclip -selection c在 Linux 上)或者甚至更好,将其重定向到文件。

... ...

Step 2 -?第2步 -? Step 3 - Profit:)第 3 步 - 利润:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM