Running Javascript file on the server using crontab

感情迁移 提交于 2019-12-23 04:52:54

问题


I have a JavaScript file which does some processing and calls a php file in the end. I need to do this entire processing on the server using a cron tab.

Can I write my JavaScript file as

<?php
    echo '<script type="text/javascript"> // JS code ... </script>';
?>

and then use crontab to run the php file?

Or

Should I use node.js to run my JavaScript file on the server using cron tab?


回答1:


After OP clarifications, what you need is a headless browser (kind of browser emulator to be run by a machine, not a human), to run your client Javascript code (with your xmlHttp requests and so).

You can find a list of headless browsers here

  • HtmlUnit - Java. Custom browser engine. Limited JavaScript support/DOM emulated. Open source.
  • Ghost - Python only. WebKit-based. Full JavaScript support. Open source.
  • Twill - Python/command line. Custom browser engine. No JavaScript. Open source.
  • PhantomJS - Command line/all platforms. WebKit-based. Full JavaScript support. Open source.
  • Awesomium - C++/.NET/all platforms. Chromium-based. Full JavaScript support. Commercial/free.
  • SimpleBrowser - .NET 4/C#. Custom browser engine. No JavaScript support. Open source.
  • ZombieJS - Node.js. Custom browser engine. JavaScript support/emulated DOM. Open source. Based on jsdom.
  • EnvJS - JavaScript via Java/Rhino. Custom browser engine. JavaScript support/emulated DOM. Open source.
  • Watir-webdriver with headless gem - Ruby via WebDriver. Full JS Support via Browsers (Firefox/Chrome/Safari/IE).
  • Spynner - Python only. PyQT and WebKit.
  • jsdom - Node.js. Custom browser engine. Supports JS via emulated DOM. Open source.
  • TrifleJS - port of PhantomJS using MSIE (Trident) and V8. Open source.
  • ui4j - Pure Java 8 solution. A wrapper library around the JavaFx WebKit Engine incl. headless modes.
  • Chromium Embedded Framework - Full up-to-date embedded version of Chromium with off-screen rendering as needed. C/C++, with .NET wrappers (and other languages). As it is Chromium, it has support for everything. BSD licensed.
  • Selenium WebDriver - Full support for JavaScript via browsers (Firefox, IE, Chrome, Safari, Opera). Officially supported bindings are C#, Java, JavaScript, Haskell, Perl, Ruby, PHP, Python, Objective-C, and R. Unofficial bindings are available for Qt and Go. Open source.

List taken from https://stackoverflow.com/a/814929/460306




回答2:


You have to choose if you want to run the javascript code in the server or in the client.

In the first case, you need to use node.js, as it is server-side. If your javascript code must be run in the client, you can install an add-on on your browser to make automatic from time to time (you can also do a program that simulate that requests).



来源:https://stackoverflow.com/questions/40531021/running-javascript-file-on-the-server-using-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!