Saturday 26 October 2019

Is this Express code sample actually a load balancer?

I've seen this code block on a handful of websites now regarding a very simple load balancer in Express. Does node handle each express instance in a separate thread under the hood? How is this code sample any different from two listeners operating in the same thread?From: https://medium.com/techintoo/load-balancing-node-js-51b854fb4f4f const body = require('body-parser'); const express = require('express'); const app1 = express(); const app2 = express(); // Parse the request body as JSON app1.use(body.json()); app2.use(body.json()); const handler = serverNum => (req, res) => { console.log(server ${serverNum}, req.method, req.url, req.body); res.send(Hello from server ${serverNum}!); }; // Only handle GET and POST requests app1.get('', handler(1)).post('', handler(1)); app2.get('', handler(2)).post('', handler(2)); app1.listen(3000); app2.listen(3001);

Submitted October 26, 2019 at 03:09PM by FullstackViking

No comments:

Post a Comment