服务器端API的基本用法,并将服务器端变量传递给客户端

时间:2021-08-22 17:59:07

I've just started my IT degree and I'm a beginner to the use of APIs (and forums like this) so I am truly sorry if my question is to vaguely explained or if it is just plain stupid :), on top of that I'm not a native English speaker :P. Okay, so I'm trying to use Google trends' api which I installed in my server with putty by using sudo npm install google-trends-api. (it can be found here https://www.npmjs.com/package/google-trends-api#installation) As I undestand it, this is a server side api so the scripts that I write with the methods provided for this api will not run on an explorer as normal js files do. There is an example that makes use of the API that I found on the page which is as follows

我刚刚开始我的IT学位,而且我是初学者使用API​​(以及像这样的论坛)所以我真的很抱歉,如果我的问题是模糊解释或者它只是简单的愚蠢:),除此之外我不是以英语为母语的人:P。好吧,所以我正在尝试使用sudo npm install google-trends-api,使用putty安装在我的服务器上的Google trend'api。 (它可以在这里找到https://www.npmjs.com/package/google-trends-api#installation)我不记得它,这是一个服务器端api所以我用这个api提供的方法编写的脚本不会像普通的js文件那样在资源管理器上运行。有一个例子可以使用我在页面上找到的API,如下所示

var googleTrends = requite('google-trends-api');

googleTrends.hotTrends('US')
.then(function(results){
    console.log(results);
})
.catch(function(err){
    console.log(err);
});

this outputs a list of 20 items on the console when I use it on node. I would like to know if there is a way to assign those results to a variable and then use that variable in a normal javascript script inside a html file. I do not know anything about node.js and the like, and I would like to actually do some research instead of asking here but I was going to use a different approach to acquire such information but now I've had to change my plans and do not have enough time and given I consider this is a fairly easy problem to resolve (maybe?) I would really appreciate it if someone could walk me through the basics of each step. THanks :) and have a nice day.

当我在节点上使用它时,它会在控制台上输出20个项目的列表。我想知道是否有办法将这些结果分配给变量,然后在html文件中的普通javascript脚本中使用该变量。我对node.js等一无所知,我想实际做一些研究,而不是在这里问,但我会用不同的方法来获取这些信息,但现在我不得不改变我的计划和没有足够的时间和考虑我认为这是一个相当容易解决的问题(也许?)如果有人能够指导我完成每个步骤的基础知识,我将非常感激。感谢,并有一个愉快的一天。

1 个解决方案

#1


0  

Your question is quite broad. Node.js is Chrome's V8 engine bundled with some libraries to do I/O and networking. This enables us to JavaScript outside of the browser and to create backend services or servers in general (in your case). I hope that you are aware of this difference :)

你的问题很广泛。 Node.js是Chrome的V8引擎,捆绑了一些库来进行I / O和网络连接。这使我们能够在浏览器之外使用JavaScript,并创建一般的后端服务或服务器(在您的情况下)。我希望你知道这个差异:)

The first thing that you have to do, is to have a look at express.js and to create a simple server. It will not be more than 20 lines of code. Then you have to enrich this with more stuff like a template engine (handlebars.js, jade etc). You have to enable the server to serve static files that will be finally your js, css and image files. Creating this simple server you will be able to serve simple html page in the first place. On top of that you should have the client side javascript that you have to write and now you can use the module above. Unfortunately, you are not able to use this module directly on a javascript file that you will write. To be able to use this module you have to transcompile this thing into javascript that browser understand*. Remember that browser does not understand the require statement and some old browsers possibly will have issues with the promises that this module is using. These are the things that should be compiled. You have to use a tool like browserify for this and the compiled file that this will extract it must be included in the scripts of your html page.

您要做的第一件事是查看express.js并创建一个简单的服务器。它不会超过20行代码。然后你必须用更多的东西来丰富它,比如模板引擎(handlebars.js,jade等)。您必须启用服务器以提供最终将是您的js,css和图像文件的静态文件。创建这个简单的服务器,您将能够首先提供简单的HTML页面。最重要的是你应该有你必须编写的客户端javascript,现在你可以使用上面的模块。不幸的是,您无法直接在您要编写的javascript文件上使用此模块。为了能够使用这个模块,你必须将这个东西反编译成浏览器理解的javascript *。请记住,浏览器不理解require语句,而某些旧浏览器可能会遇到此模块正在使用的promise的问题。这些是应该编译的东西。你必须使用像browserify这样的工具,并且它将提取它的编译文件必须包含在你的html页面的脚本中。

Maybe there are quite a lot of concepts that you are not aware of or you don't understand them but spend a bit of time to understand them.

也许有很多你不了解的概念,或者你不理解它们,但花一点时间来理解它们。

P.S.: I' ve replied under the assumption that google-trends-api module does not use things that are specific to node.js like the file-system for example.

P.S。:我假设google-trends-api模块不使用像文件系统这样的node.js特有的东西。

#1


0  

Your question is quite broad. Node.js is Chrome's V8 engine bundled with some libraries to do I/O and networking. This enables us to JavaScript outside of the browser and to create backend services or servers in general (in your case). I hope that you are aware of this difference :)

你的问题很广泛。 Node.js是Chrome的V8引擎,捆绑了一些库来进行I / O和网络连接。这使我们能够在浏览器之外使用JavaScript,并创建一般的后端服务或服务器(在您的情况下)。我希望你知道这个差异:)

The first thing that you have to do, is to have a look at express.js and to create a simple server. It will not be more than 20 lines of code. Then you have to enrich this with more stuff like a template engine (handlebars.js, jade etc). You have to enable the server to serve static files that will be finally your js, css and image files. Creating this simple server you will be able to serve simple html page in the first place. On top of that you should have the client side javascript that you have to write and now you can use the module above. Unfortunately, you are not able to use this module directly on a javascript file that you will write. To be able to use this module you have to transcompile this thing into javascript that browser understand*. Remember that browser does not understand the require statement and some old browsers possibly will have issues with the promises that this module is using. These are the things that should be compiled. You have to use a tool like browserify for this and the compiled file that this will extract it must be included in the scripts of your html page.

您要做的第一件事是查看express.js并创建一个简单的服务器。它不会超过20行代码。然后你必须用更多的东西来丰富它,比如模板引擎(handlebars.js,jade等)。您必须启用服务器以提供最终将是您的js,css和图像文件的静态文件。创建这个简单的服务器,您将能够首先提供简单的HTML页面。最重要的是你应该有你必须编写的客户端javascript,现在你可以使用上面的模块。不幸的是,您无法直接在您要编写的javascript文件上使用此模块。为了能够使用这个模块,你必须将这个东西反编译成浏览器理解的javascript *。请记住,浏览器不理解require语句,而某些旧浏览器可能会遇到此模块正在使用的promise的问题。这些是应该编译的东西。你必须使用像browserify这样的工具,并且它将提取它的编译文件必须包含在你的html页面的脚本中。

Maybe there are quite a lot of concepts that you are not aware of or you don't understand them but spend a bit of time to understand them.

也许有很多你不了解的概念,或者你不理解它们,但花一点时间来理解它们。

P.S.: I' ve replied under the assumption that google-trends-api module does not use things that are specific to node.js like the file-system for example.

P.S。:我假设google-trends-api模块不使用像文件系统这样的node.js特有的东西。