Can I call a WSGI application from Node?

Viewed 1496

I run a Python Flask server which has a few registered routes (see an example below). One route redirects the HTTP request to a WSGI application. For my project I switched to a NodeJS server and recreated the routes because Python is too slow due to its GIL.

I was in the progress of converting also the WSGI route to NodeJS but discovered, that there is not much support for it.

The WSGI protocol describes a few environment variables like wsgi.url_scheme, but on Google I can't find any examples or references about it with NodeJS (TS).

wsgi.url_scheme filetype:py 5000 Hits

wsgi.url_scheme filetype:ts 0 Hits

wsgi.url_scheme filetype:js 0 Hits

Node/TS

app.get("/wsgi", async function (req, res) {
    var env = createWsgiEnvironmentFrom(req); <--- what I would need

    var p = child_process.spawn('wsgi-test.exe', ["foo"],
        {
            stdio: [process.stdin, process.stdout, process.stderr],
            env: {...env }
        });
    }
);

Python:

@app.route('/wsgi', methods=['POST', 'GET'])
def wsgi(path):
    env = request.environ  # already WSGI compatible
    p = subprocess.Popen(args=["wsgi-test.exe", "foo"],
        env=env,
        ...)

Does this have a reason? I would assume that Node is better in scaling incoming connections so I am surprised I see so little/no examples in combination with Node

Any help is highly appreciated!

1 Answers

The simple reason is because WSGI

is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language.

Once said that it seems you just need to populate an Object with standard and WSGI environment variables as described in WSGI doc. Apart from wsgi.input and wsgi.errors (I don't know how Python can pass a stream in an environment variable, I don't know what a file-like object is) it should be quite easy. If you find problems with some environment variable, feel free to ask again.

Hope this helps.

Related