Javafx.webView not opening local page in NodeJS server

244 Views Asked by At

I have a minimal javafx webview and a nodeJs server code. But webview doesn't open its local page (http://localhost:8000/), after loading the page, the status is CANCELLED. Full sequence of statuses: READY > SCHEDULED > RUNNING > CANCELLED.

If you run the page 'http://192.168.54.1:8000' (localhost > ipv4) then it throws the error 'Connection refused by server'.

Please tell me what could be the problem?

WebView code:

import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;

public class Web {
    private JSBridge jsBridge;
    private WebEngine webEngine;
    private javafx.scene.web.WebView webView;
    private Worker<?> engineWorker;
    Web(){
        webView = new javafx.scene.web.WebView();
        webView.setPageFill(Color.TRANSPARENT);
        webEngine = webView.getEngine();

        engineWorker = webEngine.getLoadWorker();
        engineWorker.stateProperty().addListener(this::engineWorkerListener);
        webEngine.load("http://localhost:8000/");
    }

    private void engineWorkerListener(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState){
        System.out.println("WebView status: " + ov.getValue());
        if (newState == Worker.State.FAILED) System.out.println("WebView exception: " + webEngine.getLoadWorker().getException().toString());
        if (newState == Worker.State.SUCCEEDED) System.out.println("WebView loading SUCCEEDED!");
    }
}

NodeJS server code:

const http = require("http");
const host = 'localhost';
const port = 8000;

const requestListener = function (req, res) {
    res.writeHead(200);
    res.end("My first server!");
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`Server is running on http://${host}:${port}`);
});
1

There are 1 best solutions below

0
Dmitriy On

Changing this line of code solved the problem and the page opened successfully:

res.writeHead(200) -> res.writeHead('200', {'Content-Type': ''});