Here we will be making a basic REST API using the Express framework for NodeJS. I am not going in detail of how Express works, but straight to the code, which is as below. While this may look as a regular Express application, the important thing is the API can be called from any web server.
While developing APIs, the first thing came to me is, how to call this from another server?! For thats the only reason we are exposing the API. And the answer is enable CORS, cross-origin resource sharing. It simply means, make the API available or expose to these servers. we can specify the domain names individually or in this partcular case, we have exposed the API to any sever by specifying * .
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
This means, upon any API call, add these headers first. And the header says, its allowed from any *
domain. So the result is, before the API call hits any route, it first comes into this all
binding and sets the values and then goes to the respective route.
Happy Noding.