How to combine express with tcp-socket in Node.js

Viewed 3946

I implemented my first web-app with node.js and express. It works fine. The structure is as followed.

app.get('/', (request, response) => {
    response.render('home', {orders, actualPosition, path});
});

app.post('/', function(req, res){
   //do something
    res.redirect('/')
})

app.listen(8080, () => {
    console.log(`Listening on http://127.0.0.1:${port}/ !`);
})

Now I would like to communicate with a matlab-program, preferable via tcp/ip. Means I have got a matlab-program, that exchanges data with the web-application. I achieved to send data by opening a server with net on another port like that:

var server = net.createServer(function(socket) {
    //read data
    socket.setEncoding("ascii"); //set data encoding (either 'ascii', 'utf8', or 'base64')
    socket.on('data', function(data, res) {
       //update new data in browser
    });

    //send data
    socket.write('Echo server\n');
    socket.pipe(socket);
});

server.listen(8081, '127.0.0.1');

My problem now is how can I display received data via tcp/ip in the browser. Can I call app.get('/') somehow from extern to refresh the page. What are my options to get this work?

Thanks already

2 Answers
Related