最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to run javascript file on the server side? - Stack Overflow

programmeradmin2浏览0评论

Suppose I have server. A client loading an HTML file containing a javascript library will have the script executed by the browser. The problem here is that if the client's puter is slow, the processing will take a long time.

So I want to move the processing to the server side. But, instead of having to rewrite the entire javascript library into another language, I simply want to run the javascript on the server.

Googling "server side javascript" directs me to Node.JS, which in my imagination have the capability to do so. But, I cannot find a tutorial which does just that. Does this mean that there really is no easy way to do so? For example, because the javascript script may contain DOM specific things such as document.getElementById(), which does not make much sense on the server side.

Suppose I have server. A client loading an HTML file containing a javascript library will have the script executed by the browser. The problem here is that if the client's puter is slow, the processing will take a long time.

So I want to move the processing to the server side. But, instead of having to rewrite the entire javascript library into another language, I simply want to run the javascript on the server.

Googling "server side javascript" directs me to Node.JS, which in my imagination have the capability to do so. But, I cannot find a tutorial which does just that. Does this mean that there really is no easy way to do so? For example, because the javascript script may contain DOM specific things such as document.getElementById(), which does not make much sense on the server side.

Share Improve this question asked Aug 15, 2013 at 6:53 Winson TanputramanWinson Tanputraman 3,8031 gold badge17 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

There is no trivial way to simply shift processing of JS from the client to the server.

You need to break the code down into code that must run on the browser (such as, assuming you don't want the browser to load an entirely new page, DOM manipulation) and code that can run on the server.

Then you need to provide a way to pass data between the server and the browser, this is normally done via HTTP (using Ajax).

When you take input from the client you need to send it to the server in an HTTP request (instead of just passing it as an argument to a function). The server needs to read the HTTP request, process it, and make an HTTP response.

The Ajax callback then needs to parse the response and run any client side logic (such as DOM updates) in response.

Note that network munication times will impact performance.

You can't "merge" the client and server in this way. All you could do is process the data on the server and just display it in the client without any further processing. Maybe you should refresh you knowledge about HTTP and how websites are send to the clients. Without any additional tricks, like websockets, et or ajax polling, you can't access the client after you send the initial website to it. Even than you can just send data to the client.

When you want to stick to Javascript, Node.js is a good option. But even than you would need to send the data you want processed to the server, process it there and send back the processed data in JSON or "display ready" HTML.

发布评论

评论列表(0)

  1. 暂无评论