1

So I followed this answer and made it to the last step, correctly I think. But then what? I'm trying to run a node file, but it doesn't appear to be in the file listed at the PATH directory. How am I supposed to get it in that folder?

My node entry file:

'use strict';

var express = require("express");
var child_process = require("child_process");

var app = express();


app.get("/go", function(req, res, next) {
  var stream = child_process.spawn("node file.js").on("error", function(error) {
    console.log("Error!!! : " + error);
    throw error;
  });
});

app.get("/", function(req, res, next) {
  console.log("Hit main page.");
  res.end("Got me.");
});

app.listen( (process.env.PORT || 4000), function() {
  console.log("Example app listening on Heroku port " + process.env.PORT + " or local port 4000!");
});

And file.js, the file I'm attempting to open:

'use strict';

function sayHello() {
  console.log("Hello.");
}

sayHello();
Community
  • 1
  • 1
cweber105
  • 535
  • 1
  • 4
  • 21
  • Please include the actual code you are using pasted into your question. Questions that rely on outside links as the only source of the relevant information needed to understand and answer this question are not allowed here and are considered off-topic. This is partly because external links tend to change or disappear over time which will then render this question and any answers it gets as useless as a lasting reference source. Please paste the relevant code into your question. – jfriend00 Oct 06 '16 at 23:15
  • Right, my bad. Updated it with the two files. – cweber105 Oct 07 '16 at 13:23

1 Answers1

3

In your spawn method, you should have:

app.get("/go", function(req, res, next) {
  var stream = child_process.spawn("node", ["file.js"]).on("error", function(error) {
    console.log("Error!!! : " + error);
    throw error;
  });
});
Bwaxxlo
  • 1,860
  • 13
  • 18