1

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?

mister martin
  • 6,197
  • 4
  • 30
  • 63
AAA
  • 47
  • 2
  • 12
  • javascript is executed by the client (browser) not the server. – mister martin Nov 10 '16 at 15:19
  • Use node.js, as you said. Javscript runs in the browser which obviously won't suit you trying to run it on a server. – Reinstate Monica Cellio Nov 10 '16 at 15:20
  • @mistermartin I need to run this my code (which is in javascript and call a php file) every 30 minutes in the background. That's why thought of using a cronJob. – AAA Nov 10 '16 at 15:23
  • @AAA I'm not familiar with node.js, so I added that to your question tags. I do know javascript will *not* execute from cron as you're suggesting. – mister martin Nov 10 '16 at 15:30
  • @Archer My concern is - my java script file sends some data to a php file. The php file then sends the data over to a server. Is this all possible together if I deal with nodeJS? – AAA Nov 10 '16 at 15:43
  • So you need to send some data to a server, and cut out the PHP part. Just search for making http posts using Node.js (assuming it _is_ a http post). – Reinstate Monica Cellio Nov 10 '16 at 15:45
  • @Archer Let me explain - `My java script gets some data from one server, does from processing with the received data and then sends the processed data to a php file(The php file in turn sends the data to another server)` All this needs to be done every 30 minutes – AAA Nov 10 '16 at 15:49
  • That changes nothing. Use Javascript, running on Node.js, to get the data, process it and then send it. Is it a HTTP post? – Reinstate Monica Cellio Nov 10 '16 at 15:49
  • Yes.. A xmlHttp request (in Javascript) to get data in the first step – AAA Nov 10 '16 at 15:50
  • These will point you in the right direction... [Simple HTTP GET/POST Request in Node.js](http://samwize.com/2013/08/31/simple-http-get-slash-post-request-in-node-dot-js/) – Reinstate Monica Cellio Nov 10 '16 at 15:52

2 Answers2

3

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

Community
  • 1
  • 1
greuze
  • 4,250
  • 5
  • 43
  • 62
1

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).

greuze
  • 4,250
  • 5
  • 43
  • 62
  • I can run it either on the server or the client. I just need the code to be running every 30 min not needing me to do anything once I initiate the execution. Also my concern is - my java script file sends some data to a php file. The php file then sends the data over to a server. Is this all possible together if I deal with nodeJS? – AAA Nov 10 '16 at 15:41
  • What do you want to do inside your javascript code? It is not the same to run the code in the server or in the client. If you use node.js, you don't need php, as they do the same (running code in a server). – greuze Nov 10 '16 at 15:50
  • My java script gets some data from one server, does from processing with the received data and then sends the processed data to a php file(The php file in turn sends the data to another server) All this needs to be done every 30 minutes – AAA Nov 10 '16 at 15:51
  • Why don't you do a loop in your PHP server that is fired every 30 minutes, gets the data from there, process it and send to destination server? – greuze Nov 10 '16 at 15:56
  • The entire code works well when I put the java script in the body tags of a html file. And open this file as webpage. The only problem was, I had to keep this web page open in my browser all the time. That's why was thinking of an alternative to run this entire thing in the background – AAA Nov 10 '16 at 16:01
  • I an truing to convert my entire JS code into a PHP code. In my JS code, I have some HTTP Get and POST requests. Can you point me in the right direction as to how can I get some data from one server using PHP? – AAA Nov 10 '16 at 19:12
  • The question is not clear at all, it can be translated code without knowing what it is doing. You should close it and open a new one explaining the functionality required, to get help doing it in PHP, node.js or whatever. – greuze Nov 11 '16 at 07:35