Is it possible to run javascript as cgi script on Apache? - javascript

I managed to configure my Apache to run cgi scripts and I tried to use javascript to generate output.
#!C:\php\php.exe
<script>
document.write("Hello world!");
</script>
However this only sends the contents and the document.write is run on the client. I'd like to send Hello world! by javascript.
I know there is nodejs. I wonder if there is a module for Apache or some settings that enables running javascript on the server and send only the contents of document.write?

CGI explicitly spawns an external process, so would use mod_cgi. You could then spawn a Node.js process.
You would, however, need to write a real CGI program and not use a fragment of HTML with embedded JS that depends on browser APIs.
#!/usr/bin/env node
process.stdout.write("Content-Type: text/plain\r\n\r\n");
process.stdout.write("Hello, world");
CGI isn't very efficient, and Node would be an odd choice of language. If you need to go via Apache then it would usually make more sense to run a server using Node and then use mod_proxy to access it via Apache.

Related

Run HTML file via Bash? Possible?

I have a html file that when run in a browser such as Chrome and that contain javascript instructions, it sends the "emit" message to my websockets server and displays the value on that page.
Is there a way to call this same html file from a bash script as I'm wanting to insert data into a MySQL database which will ultimately call that html file to send an update to the websocket.
Hopefully that makes sense but hopefully there is a way to do it too :)
If you are only focused on rendering the HTML page (and not interacting with it via buttons or something) you may find this link helpful: Running HTML from Command Line
If you try to execute javascript instruction in your server without using a browser, I recommand you to use Node.JS with a real js script without html.
Otherwise you can try to run an html file with js instruction inside using something like phantomjs but I think is less performant than using Node correctly.
EDIT
It is Javascript yes, but I need to "Import" the socket.io.js file into the same script I have created and my browser I'm having to use doesn't support the new Javascript import methods. So I'm writing as HTML which calls the Javascript, otherwise I would use nodejs
I think you can import the socket.io lib in node application using npm.
https://www.npmjs.com/package/socket.io
The documentation says that you can use a client inside a node script too :
Socket.IO enables real-time bidirectional event-based communication. It consists in:
a Node.js server (this repository)
a Javascript client library for the browser (or a Node.js client)

How does OS cache fat-jar content?

There is a way to deploying java applications within fat-jar. You compile and build all the files and put into single file, your backend with REST api and frontend - javascript files. As a server you use embedded one - jetty, tomcat, scalatra etc. How does (and where) operating system cache the content of jar file while running java -jar? (I'm interested mostly in ubuntu and centos) I ask in context of serving that javascript file to the client. [In worst imaginable case the system might extract the whole jar file each time the http request comes, that would be horrible]

How to execute phantomjs file browser?

I'm new to phantomJs. I've installed phantomjs in my system(Ubuntu) and executed a sample code "test.js" in terminal like,
test.js:
console.log("WELCOME TO PHANTOMJS");
phantom.exit();
In terminal
phantomjs test.js
its executed fine shows expected output.
NOw my question is, Is there any possibilites to execute above js file in a browser by importing that js file in a html file like,
<script src="test.js".....></script>
If is it possible means how to do?? Please help me.
Short Version
You can't
Detailed Version
In its current state, PhantomJS is a standalone process and needs to have the full control (in a synchronous matter) over everything: event loop, network stack, and JavaScript execution, ... yes you write scripts in Javascript but it's simply because it exposes a JavaScript API (also for coffee script).
"How to execute a nodejs module in browser?" It's pretty the same problem and you simply can't because of phantomjs/nodejs dependencies : what is webpage module in your favorite the browser ? But it's still possibe to share pure JS library.
So, you can't directly reference a phantomjs script in your browser, but you can communicate with a PhantomJS instance using HTTP GET/POST with Web Server module. HEre is a topic onInter Process Communication found on the Wiki.

Interfacing with Local Perl Script from a Web UI

Given that I have a local Perl script that prints "Hello" in a windows command prompt.
I want to make a Web Interface that when I click a button on an HTML page using Javascript or something, I'd be able to execute this local Perl Script. I have tried hosting the page on Localhost in an Xampp Apache server however this uses the Perl installed on Xampp.
Is there anyway to interact with local Perl from a Web User Interface? i.e. Running a perl script in the local shell from a Web UI.
I usually run a perl script that works as a primitive web server. Upon startup it prints a URL which can be used to access it.
This can be done with the following modules:
use HTTP::Daemon;
use HTTP::Status;
use HTTP::Response;
in about 60 lines of code.
Can't post my code, but here is a more elaborate sample (it uses fork, which is an improvement over what I have): http://www.perlmonks.org/?node_id=415908
Look into Node JS... it can call perl from the webpage, is very very simple to set up and get a localhost site going, and is free.

print to file javascript

I'm wondering.. how could I output a string to a file (and create said file) in javascript?
I'm using this for Chrome extension development.
You can't create, read, or modify files on the computer using browser based client side Javascript for security reasons.
If you want to do this from within a browser, it won't work except you might use activex or java (via plugins).
Or you fetch yourself the spidermonkey (mozilla's javascript engine) source code and compile it into a command line application. You will get file access this way.
Cheers,
-S
You post the info to a http handler (ashx in asp.net, or just a php site), which then writes the info to a file.

Categories

Resources