3

I am Trying to set up a simple Node.Js Server which calls a Python Script on hitting a url. Given below are the python and Node js server file.

When I hit the server url. The page loads! but then the server crashes and It give me the following error (in the cmd prompt):

    Server listening on: http://localhost:8080/
this is here so we are in
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: spawn C:UsersShubhamAnaconda3libos.py ENOENT
    at exports._errnoException (util.js:837:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at doNTCallback2 (node.js:429:9)
    at process._tickCallback (node.js:343:17)

and this in the Web browser console:

GET http://localhost:8080/favicon.ico net::ERR_CONNECTION_REFUSED

I have checked similar issues. But they are a little different and their fixes don't help me. I HAVE CLEARED THE CACHE AND CHECKED.

Python file:

import sys
def runForFun(artist, song, lyrics):
    if lyrics is None:
        print("artist:" + artist)
        print("song:"+song)
        print("lyrics:"+lyrics)

        theme = "theme"
        return theme
    else :
        print("lyrics: "+lyrics)


try:    
    runForFun(sys.argv[0], sys.argv[1], sys.argv[2])
except IndexError:
    print('Please supply arguments')

Node js file

//Lets require/import the HTTP module
var http = require('http');
var PythonShell = require('python-shell');

//Lets define a port we want to listen to
const PORT=8080; 

//We need a function which handles requests and send response
function handleRequest(request, response){


    var options = {
      mode: 'text',
      pythonPath: 'C:\Users\Shubham\Anaconda3\lib\os.py',
      pythonOptions: ['-u'],
      scriptPath: 'C:\Users\Shubham\Google Drive\Capstone\Theme Extraction',
      args: ['value1', 'value2', 'value3']
    };

    console.log("this is here so we are in");

    PythonShell.run('runPython.py', options, function (err, results) {
    if (err) throw err;
    console.log('results: %j', results, 'finished');
});
    response.end('It Works!! Path Hit: ' + request.url);
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s/", PORT);
});
6
  • Just read that error.. Error: spawn C:UsersShubhamAnaconda3libos.py Does that look like a file path? You likely need to escape the backslashes Commented Apr 18, 2016 at 17:43
  • Also, I think pythonPath needs to point at the python executable file, not a python script. Commented Apr 18, 2016 at 17:45
  • How is var options in your code something that is internal? You need C:\\Users, for example Commented Apr 18, 2016 at 17:46
  • No not the var options, I was replying to your 1st comment. Working on your second suggestion. Thanks. Commented Apr 18, 2016 at 17:52
  • My comments are related. You error is referring to the var options where you have unescaped backslashes at pythonPath: 'C:\Users\Shubham\Anaconda3\lib\os.py' Commented Apr 18, 2016 at 17:55

1 Answer 1

2

You should escape the slashes in the file paths and I think the pythonPath should point at a Python executable file, not a script.

Therefore, the correct setup would look like this

var options = {
      mode: 'text',
      pythonPath: 'C:\\Python\\pythonw.exe',
      pythonOptions: ['-u'],
      scriptPath: 'C:\\Users\\Shubham\\Google Drive\\Capstone\\Theme Extraction',
      args: ['value1', 'value2', 'value3']
    }; 

Though, overall, unless you specifically need Node.js, why not use a Python web framework that you could simply import your script to run it?

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

1 Comment

I was just trying to learn. I will try python web framework as well. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.