So I would like to do something like:
app.On_All_Incoming_Request(function(req, res){ console.log('request received from a client.'); });
the current app.all()
requires a path, and if I give for example this /
then it only works when I'm on the homepage, so it's not really all..
In plain node.js it is as simple as writing anything after we create the http server, and before we do the page routing.
So how to do this with express, and what is the best way to do it?
How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.
Express. js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions.
As is, node. js can process upwards of 1000 requests per second and speed limited only to the speed of your network card. Note that it's 1000 requests per second not clients connected simultaneously. It can handle the 10000 simultaneous clients without issue.
Your code is non-blocking because it uses non-blocking I/O with the request() function. This means that node. js is free to service other requests while your series of http requests is being fetched.
Express is based on the Connect middleware.
The routing capabilities of Express are provided by the router
of your app and you are free to add your own middlewares to your application.
var app = express.createServer(); // Your own super cool function var logger = function(req, res, next) { console.log("GOT REQUEST !"); next(); // Passing the request to the next handler in the stack. } app.configure(function(){ app.use(logger); // Here you add your logger to the stack. app.use(app.router); // The Express routes handler. }); app.get('/', function(req, res){ res.send('Hello World'); }); app.listen(3000);
It's that simple.
(PS : If you just want some logging you might consider using the logger provided by Connect)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With