Search code examples
node.jsexpressmocha.jscodemirror

Create a file on back-end, from the text coming from front-end


I'm trying to make an app that grabs code from two different code editors (using angular-ui-codemirror), one for the code to be tested and the other for the test code, and outputs the output of the tests back onto the site.

As of now, I can get the text from both code editors concatenated and console.logged out in the browser.

I'm wondering how I could take that code and save it into a file on the back- end, so that I could spawn a child-process that runs tests on it and does something with the stdout.

I'm using angular, node and express for coding and for testing, I am using mocha.


Solution

  • fs.writeFile('./yournewfile.js', newCode, function (error) {
      if (error) return callback(error)
    
      var Exec = require('child_process').exec;
    
      // change node to mocha if your newCode is mocha test code
      Exec('node ./yournewfile.js', function (error, stdout, stderr) {
        // do something here
      })
    })