简体   繁体   中英

Open file in one directory to another in node js

I created program python-shell.js located in Controllers directory. This program gives some data.

python-shell.js : CODE:

 var myPythonScriptPath = 'script.py';

// Use python shell
    const {PythonShell} = require("python-shell");
    var pyshell = new PythonShell(myPythonScriptPath);

    module = pyshell.on('message', function (message) {
    // received a message sent from the Python script (a simple "print" statement)
        console.log(message);
    });

    // end the input stream and allow the process to exit
    pyshell.end(function (err) {
    if (err){
        throw err;
    };

    console.log('finished');
    });

I created another server.js file outside of the controllers directory. It is to run the python-shell.js file.

server.js : CODE

var app = express();
var path = require('path');

app.use(express.static(path.join(__dirname, 'Controllers')));

app.get('/', function(req,res){
    res.sendFile(__dirname + '/Controllers');
});
app.listen(3000,function() {
console.log('Listening');
})

But this server.js code gives me the code that I wrote in the "python-shell.js" file. But I want server.js to give the data that "python-shell.js" gives when run independently instead of giving the code.

As I understood. You would like to get data from a Python module when there is a request coming to your server, instead of just serving plain text source files.

As you can see the line res.sendFile(__dirname + '/Controllers'); would send file itself, it would not execute it and provide whatever your file sent to the console.

To achieve what I stated earlier you would need to import your JS file (in commonjs this is called requiring the module), and then call functions to get your data from there (if you want to separate server logic with python shell logic).

Inside python-shell.js you would need to get hold of res object provided as a part of app.get('/', function(req, res) {}) callback. So I would restructure code in following way:

python-shell.js

// Note that script.py refers to /script.py, not /Controllers/script.py
// As code bellow won't be called from within python-shell.js. More on that later
const myPythonScriptPath = 'script.py';

// Use python shell
const { PythonShell } = require("python-shell");

// From this file (module) we are exporting function 
// that takes req and res parameters provided by app.get callback
// Note that this file is not executed by itself, it only exposes
// a single function to whom is gonna import it
module.exports = function (req, res) {
    const pyshell = new PythonShell(myPythonScriptPath);

    // Create buffer to hold all the data produced 
    // by the python module in it's lifecycle
    let buffer = "";

    module = pyshell.on('message', function (message) {
        // received a message sent from the Python script (a simple "print" statement)
        // On every message event add that chunk to buffer
        // We add \n to the end of every message to make it appear from the new line (as in python shell)
        buffer += message + '\n';
    });

    // end the input stream and allow the process to exit
    pyshell.end(function (err) {
        if (err) {
            // If error encoutered just send it to whom requested it
            // instead of crashing whole application
            res.statusCode = 500;
            res.end(err.message);
        }
        // res.end sends python module outputs as a response to a HTTP request
        res.end(buffer);
        console.log('finished');
    });
}

And inside of server.js:

const express = require("express")
const path = require('path');

// We are importing our function from Controllers/python-shell.js file 
// and storing it as variable called pythonShellHandler
const pythonShellHandler = require("./Controllers/python-shell")

const app = express();

// express.static will serve source files from the directory
app.use(express.static(path.join(__dirname, 'Controllers')));

// Here we are passing our imported function as a callback
// to when we are recieving HTTP request to the root (http://localhost:3000/)
app.get('/', pythonShellHandler);
app.listen(3000, function () {
    console.log('Listening');
})

As you can see. I restructured your code so that python-shell.js exports to the other .js files a single function that takes Express's req and res parameters. And from within server.js I import this function.

This Node.js module system can be a bit hard to get gist of at first. If you are still confused, I recommend reading about node modules

Note: if your python script takes way too long to finish, or doesn't finish at all. Browser will just timeout your request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM