2

I want to write a piece of C software (like prime number factorization) and place it on my web-server (based on Node.js). Then I would like to have an HTML-document with a form and a button.

When the button is clicked the C-program should be executed (on the server) with the a text-field of the form passed as input parameter. The output of the C-program should be passed to an HTML-textfield again.

In PHP something like this is possible:

<?php exec('~/tools/myprog.o'); ?> 

How would I go about something like this in Node.js (Express.js is fine as well). Especially the input/output redirection?

1
  • use shelljs Commented Feb 24, 2016 at 22:37

1 Answer 1

2

I feel that your question is 2 part.

  1. PHP's exec equivalent for node.js
  2. Execute C program and provide its out put using node.js.

Sticking to pure node.js, you would be able to execute and get access to stdout and stderr with following code.

var exec = require('child_process').exec; exec("ls -l", function(error, stdout, stderr) { console.log(error || stdout || stderr); }); 

For #2, I am not sure if you could directly execute a "c" program, so I guess you'd need to do something like this

 var exec = require('child_process').exec; exec("gcc ~/tools/myprog.c -o prime && ./prime", function(error, stdout, stderr) { console.log(error || stdout || stderr); }); 

Above code is definitely not thread safe and if concurrent requests are made, it will break, Ideally you'd not compile it for every request, you'd compile and cache for first request (you can match time stamp of source and compiled output, if compiled output is absent or its time stamp is less - earlier then the source code then you need to compile it) and then use cached output on subsequent requests.

In express you can do something like below

var exec = require('child_process').exec; router.get('/prime/:num',function(req, res, next){ exec("gcc ~/tools/myprog.c -o prime && ./prime " + req.params.num, function(error, stdout, stderr) { if(error){ res.status(400).json({'message': error.message}); return; } if(stderr){ res.status(400).json({'message': stderr}); return; } res.status(200).json({'result': stdout}); }); }); 

You can play with above code snippets and get what you are looking for.

Hope this helps. Please let me know if you need anything else.

Sign up to request clarification or add additional context in comments.

3 Comments

Why not just compile the C program in the application's .js entry file. That way you could prevent the app from booting if the compilation threw an error? You could even cache the program meta using app.set('c-program-verson', 'here') for later validation. In any event, it's probably not a good idea to compile a program during application lifecycle. This should most definitely be done in your deploy (or build process if you're transpiling JS) process. You could leverage npm postinstall or npm prestart if using npm scripts.
@Seth I agree with you, in question he is trying to execute a c program directly, so I just mentioned that you won't be able to directly execute c program, you'd need to compile it. So In my response I have just showed a way how to compile from within node.js, you can implement it in gulp / grunt task so as on build code gets compiled into a folder and the it directly executes in request. My snippets are "just to provide an idea" what will need to be done in order to get desired output.
Thank you, so much. I actually meant an already compiled Cprogram (executable) and just had a typo in my question. This seems like a solid answer. Accepted!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.