Nodejs Talking To C Executable 1 ============================================================= :Date: 21 Sep 2019 What is the idea? ----------------------- - My boss prefers to implement webserver in C by himself, but I think people have already spent tons of time making the state of art **nodejs**, it doesn't make sense not to use it. And since I am not a engineer but a physicist, I don't want to spend too much time on the already-solved-for-decades engineering problem. So I will just use Nodejs without thinking - However we will still use C to do the basic memory mapping, as it is already done. - So the problem now is to make Nodejs talk to C executable, turns out to be very easy Install Nodejs and Clion ---------------------------- - I recommend using Clion for C part, if you are a student, you can get free license with a :code:`edu` email address - For Nodejs on Ubuntu .. code-block:: console curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt install nodejs node -v - For Nodejs on Angstrom Linux and ARMv7 - `First download from here `_ - `Then read this `_ C Part --------------------- .. code-block:: c #include int main(int argumentCount, char ** arg) { //if you ran your program like this: // ./program hello world // Then: // argumentCount = 3 // argv[0] = "./program" // argv[1] = "hello" // argv[2] = "world" printf("argv[0] = %s\n",arg[0]); printf("argv[1] = %s\n",arg[1]); printf("argv[2] = %s\n",arg[2]); return 0; } In Clion, go to :code:`Build -> Build Project`, then the executable file will be in folder :code:`cmake-build-debug`, copy this file to same directory as your Nodejs code (below) Nodejs Part ------------------ - make a file named :code:`app.js`, and put the following in it: .. code-block:: jsx const { execFile } = require('child_process'); const child = execFile('./hello', ["hello","world"], (error, stdout, stderr) => { if (error) { throw error; } console.log("stdout:"); console.log(stdout); }); - then command: :code:`node app.js`