简体   繁体   English

从一个 GET 请求中导出输入查询并在单独的请求中使用它们

[英]export input query from one GET request and use them on separate requests

I am trying to use the input of the user on my RESTful API in the query for one GET request and use that data to randomize a number and use that info for the rest of the application.我正在尝试在我的RESTful API上使用用户的输入来查询一个GET请求,并使用该数据随机化一个数字并将该信息用于应用程序的 rest。 any guidance about best solution to store the values and use them on different blocks throughout the application is appreciated.任何有关存储值并在整个应用程序的不同块上使用它们的最佳解决方案的指导都值得赞赏。 Part of the code sample is as below:部分代码示例如下:

    app.get("/start", (req, res) => {
        const numberOfFigures = req.query.figures;
        const randomBaseNumber = Math.random();
        const theNumber = (randomBaseNumber * 10 ** numberOfFigures).toFixed(0);
        res.send(`guess the ${numberOfFigures} digit number!`);

    app.get("/guess", (req, res) => {
        let guessedNumber = req.query.guess;
        if (guessedNumber !== theNumber) {
            console.log("Wrong! Guess again");
        }

I am trying to use for instance theNumber value from /Start request in the /guess request!例如,我正在尝试在 /guess 请求中使用 /Start 请求中的 Number 值!

You need to keep theNumber variable outside of the scope of both callbacks eg您需要将 Number 变量保留在两个回调的theNumber之外,例如

    let theNumber = 0
    app.get("/start", (req, res) => {
        const numberOfFigures = req.query.figures;
        const randomBaseNumber = Math.random();
        theNumber = (randomBaseNumber * 10 ** numberOfFigures).toFixed(0);
        res.send(`guess the ${numberOfFigures} digit number!`);

    app.get("/guess", (req, res) => {
        let guessedNumber = req.query.guess;
        if (guessedNumber !== theNumber) {
            console.log("Wrong! Guess again");
        }

I would also use POST for the /guess endpoint我也会将 POST 用于/guess端点

The following 3 ways of storing data come to mind:想到以下 3 种存储数据的方式:

  1. Declare theNumber as a global variable.将 theNumber声明为全局变量。
  2. Store the variable in a json and import it when needed (eg: fs module).将变量存储在 json 中,并在需要时将其导入(例如:fs 模块)。
  3. Store it in a database (eg: MongoDB).将其存储在数据库中(例如:MongoDB)。

Method 1 is the easiest way.方法1是最简单的方法。 The variable will be accessible from all the routes, and it will be erased if your app restarts.该变量将可以从所有路由访问,并且如果您的应用重新启动,它将被删除。

let theNumber = 0;
...
app.get("/start", (req, res) => {
    theNumber = ...
    ...

Methods 2 and 3 will make the variable, once created, persist even if the app restarts.方法 2 和 3 将使变量在创建后保持不变,即使应用程序重新启动也是如此。 However, for this simple use case, I'd say they're overkill.但是,对于这个简单的用例,我会说它们是矫枉过正的。

暂无
暂无

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

相关问题 如何使用字段作为来自一个 graphql 查询的过滤器,以获取单独查询中的流体图像? - How can I use a field as a filter from one graphql query to get a fluid image thats in a separate query? 如何从网址中获取多个图片并将它们分别放在javascript的单独div中? - How to get multiple images from url and place them in separate div in javascript one by one? 从多个项目获取ID以在AJAX请求中使用它们 - get an id from several item to use them in an AJAX request 从 localStorage 获取项目并导出它们 - Get items from localStorage and export them 从获得 <input> id =和value =的动态值,并使它们成为变量以在javascript中使用它们 - Get from <input> dynamic values of id= and value= and make them variables to use them in javascript 在 Axios 请求中将查询与正文分开 - Separate query from body in Axios request 许多相同的输入-&gt;对一个请求的表单/对许多请求的许多表单? - Many same input -> form to one request / many forms to many requests? Angular:从一个请求中获取Cookie并在另一个请求中使用 - Angular: get cookie from one request and use in another 如何从输入复选框获取值以在查询中使用它来显示信息 - How to GET value from an input checkbox to use it in a query to show information 在 React JS 中,当其中一个输入字段发生更改时如何从多个输入字段中获取值 - In React JS, how to get value from multiple input fields when one of them is changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM