在服务器端运行 javascript 的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4052515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:16:36  来源:igfitidea点击:

Easiest way to run javascript on server side

javascriptserver-side

提问by Shawn

I have an application which runs on mobile devices such as android, and I have some third party traffic tracking js code to track the usage of my application. So everytime a user opens up my application the js code sends a ping to the 3rd party tracking server. However I'm not sure if there's an easy way to embed the js code directly into my application that's not browser based and couldn't interpret js code. So I may have to execute the js code on the server side. The 3rd party code is like this :

我有一个在移动设备(例如 android)上运行的应用程序,并且我有一些第三方流量跟踪 js 代码来跟踪我的应用程序的使用情况。因此,每次用户打开我的应用程序时,js 代码都会向第 3 方跟踪服务器发送 ping。但是,我不确定是否有一种简单的方法可以将 js 代码直接嵌入到我的不是基于浏览器且无法解释 js 代码的应用程序中。所以我可能不得不在服务器端执行js代码。3rd 方代码是这样的:

<script type="text/javascript" src="http://www.3rdparty.com/tracking.lib.js"></script>
<script type="text/javascript">
   var instance = new Tracking();
   instance.run();
</script>

What's the easiest way to run this code on server side?

在服务器端运行此代码的最简单方法是什么?

Thanks in advance.

提前致谢。

回答by mikerobi

There are several solutions for running server side javascript (Node.JS, or something based on Rhino), but that code is probably dependent on functionality that only exists in a browser.

有几种运行服务器端 javascript(Node.JS或基于Rhino 的东西)的解决方案,但该代码可能依赖于仅存在于浏览器中的功能。

EDIT

编辑

I answered the question, but I didn't really address the problem. Even if the code will execute run on the server, it won't work as intended. You really need to come up with a better solution for tracking usage of a non web based application.

我回答了这个问题,但我并没有真正解决问题。即使代码将在服务器上执行运行,它也不会按预期工作。您确实需要想出一个更好的解决方案来跟踪非基于 Web 的应用程序的使用情况。

The question you should have asked is: How can I track usage of an android application?

您应该问的问题是:如何跟踪 android 应用程序的使用情况?

If you are already running a server, they you could write a simple web service that logs usage, you just need to open a HTTP connection from within your app.

如果您已经在运行服务器,他们可以编写一个简单的 Web 服务来记录使用情况,您只需要从您的应用程序中打开一个 HTTP 连接。

I would be surprised if there aren't existing solutions, but I'm not an Android guy, so I wouldn't know.

如果没有现有的解决方案,我会感到惊讶,但我不是 Android 人,所以我不知道。

I would suggest deleting this question and replacing it with one targeting what you are actually trying to accomplish.

我建议删除这个问题并将其替换为一个针对您实际尝试完成的问题。

回答by Christian Kuetbach

I'm not sure if it is really possible.

我不确定这是否真的可能。

If it is a Java-Server you can use the BeanScripting Framework, but:

如果它是 Java 服务器,您可以使用 BeanScripting 框架,但是

If the Javascript code does some calculation like Screen-Size, Browser etc. You won't get that information.

如果 Javascript 代码进行了一些计算,例如屏幕大小、浏览器等。您将无法获得该信息。

You can't get the IP nor the HTTP-Client (Which Browser etc.).

您无法获得 IP 或 HTTP 客户端(哪个浏览器等)。

I Don't know the JavaScript code, so I may be wrong.

我不知道 JavaScript 代码,所以我可能是错的。

In fact, the Javascript code will call your Server to store the information. You can reverse engeneer the infromation sent, by using a Network-Sniffer.

实际上,Javascript 代码会调用您的服务器来存储信息。您可以使用网络嗅探器对发送的信息进行逆向工程。

回答by tgkprog

'So I may have to execute the js code on the server side. ' no need it to run on a nclient side browser, or browser lke component in your native app - "Web view" or ask them for a REST API, get info of handset from native code and call the API (web service all over http in most cases)

'所以我可能不得不在服务器端执行js代码。' 不需要它在 nclient 端浏览器或本机应用程序中的浏览器组件上运行 - “Web 视图”或向他们询问 REST API,从本机代码获取手机信息并调用 API(Web 服务通过 http大多数情况下)