问题
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