node.js basic auth in express.js

The first 2 lines import express node module and create an express app instance.
The 3rd line import the basic-auth node module.
The auth function takes 3 parameters, req for request object, res for response object and a callback function next which is called upon successful authentication.
The first app.get() calls the get function from the express app instance. It gives 3 arguments, the first one is an url path, the second one is the auth function defined above, and the third is a function to handle the incoming request. When a get request is made to localhost:3030/auth, it first calls the auth function, if the auth function returns the callback function next, it will then go on to execute the function that was passed in as the thrid parameter in the app.get(). If the auth function identifies the credentials are not valid, it sends a 401 not found and no callback is returned.

The express function all, app.all() can be used to which url path need the authentication.app.all("*", auth); will make all the endpoints to require authentication.app.all("/admin/*"); will make all the endpoints starting with localhost:3030/admin/ to require authentication.app.all("/auth", auth); will make the endpoint localhost:3030/auth to require authentication.