简体   繁体   English

如何从函数到 json 值获取结果?

[英]How can I get the result from function to json value?

I'm so new to node.js I'm trying to make an interval value in node.js And I can invoke a function when I reach the exact URL, however, I do not know how to pass the console.log value to JSON value.我对 node.js 很陌生 我正在尝试在 node.js 中创建一个间隔值 我可以在到达确切的 URL 时调用一个函数,但是,我不知道如何将 console.log 值传递给JSON 值。

this is my code:这是我的代码:

 // Import packages const express = require("express"); // App const app = express(); const port = 8000; app.get("/", (req, res) => { function doSomething() { console.log(Math.round(Math.random() * (3000 - 500)) + 500); } const loop = () => { setTimeout(() => { doSomething(); loop(); }, 1000); }; loop(); res.json({ random: doSomething() }); }); // Starting server app.listen(port, () => console.log(`listening on port ${port}!`));

I'd like to pass the random number in doSomething function to res.json` inside.我想将doSomething function to的随机数传递doSomething function to res.json` 里面。

So, this is what I tried :所以,这就是我尝试过的:

 // Import packages const express = require("express"); // App const app = express(); const port = 8000; let number; app.get("/", (req, res) => { function doSomething() { number = Math.round(Math.random() * (3000 - 500)) + 500; return number; } const loop = () => { setTimeout(() => { doSomething(); loop(); }, 1000); }; loop(); res.json({ random: doSomething() }); }); // Starting server app.listen(port, () => console.log(`listening on port ${port}!`));
However, the above code is working only I refresh the page, I'd like to change it every second without refresh. 但是,上面的代码仅在我刷新页面时才有效,我想在不刷新的情况下每秒更改一次。

Thank you in advance先感谢您

I think it would be better to invoke your function from the client and using setInterval instead of setTimeout otherwise it only will run it once.我认为最好从客户端调用您的函数并使用setInterval而不是setTimeout否则它只会运行一次。

Keep your server code like this:保持您的服务器代码如下:

// Import packages
const express = require("express");
// App
const app = express();
   

const port = 8000;

let number;

app.get("/", (req, res) => {
  function doSomething() {
    number = Math.round(Math.random() * (3000 - 500)) + 500;
    return number;
  }

  res.json({ random: doSomething() });
});
// Starting server
app.listen(port, () => console.log(`listening on port ${port}!`));

Then, in a js loaded from the client (browser), you could do something like this:然后,在从客户端(浏览器)加载的 js 中,您可以执行以下操作:

setInterval(() => {
  fetch("http://localhost:8000/")
    .then(res => res.json())
    .then(res => console.log(res))
    .catch(err => console.log("err", err))

}, 1000)

That said, using setInterval may not be the best thing.也就是说,使用setInterval可能不是最好的选择。 I would recommend you to create a HTML <button> or an <a> and set up and event listener to handle the click event.我建议您创建一个 HTML <button>或一个<a>并设置事件侦听器来处理单击事件。 Then, when the user clicks the button, you could invoke a function to execute the fetch operation.然后,当用户单击按钮时,您可以调用一个函数来执行提取操作。

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

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