Create an API in Node, Express & MongoDB part-2
By: Rizwan Akram
Tags: JavaScript, Node For Begginners, Node Js Tutorial, Nodejs, Node

Now, we have an issue let me show you that first,

Let just go to link other than http://localhost:3000/api/contacts

For Example:

http://localhost:3000

or

http://localhost:3000/ap

or anything else.

we will get an error, see how it looks

Let’s solve it,

So, now we will create a custom error handler that will handle all the requests that we haven’t defined in our routes and give a proper error response.

first we will create a middleware function that will generate a custom error

Here we have created a custom error message middleware that will call our Custom Error Handling middleware using next().

Express provides custom error handling middleware function that must be added at end of all the other middleware functions. What happens is when we don’t get a relevant route it will create a custom error and then finally funnel it through our custom error middleware and give us a generalise error response.

Let’s now see or chrome that what happens if user hits a wrong route

For example:

http://localhost:3000

or

http://localhost:3000/api, etc

Checking it with another wrong route.

Other than our route defined in the routes folder, for any other route, we will get an error message of not found, with error code 404, because thats how we have defined our custom error message and status.

So, good to see you reaching this much, that’s a lot.

We have now implemented both logging and error handling in our API.

These are the base things that will make your API more useful and glitch-free.

Finally, let’s solve the cors issue.

Q: What is cors?

A: Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin.

Above is the technical answer but let me explain you and show you an example,

Let’s assume our API is running at port 3000 and someone has created an app that is using our API and his front-end is running at port 4000.

Now, try to make a fetch() or an XHR call from the front-end to API running on port 3000, will defiantly get a CORS error. This is because if your front-end is running at port 3000 it can’t make a request to another port until the API allows it. Till now our API can’t do that. If you make a request to our API from the different port it will through a CORS error.

Let’s solve it.

This will always be the first middleware of our API to funnel all requests through it.

What this middle-ware will do is, funnel all the client request through our CORS middle-ware and allow CORS. The “ * ” in the first line of the middle-ware means that now any website can access it, you can also explicitly define the website URL there, then only that specific URL can access the resources of API.

Finally, we have improved our API to a great extent.

Now our API can be used by any client, without CORS issues, proper logging, and complete error handling.

So, good to see you this far.

In the next part, we will connect our API to a database ( MongoDB ). I will show you the easiest way to integrate it and use it. We will also learn little about MongoDB and how we will use it in our project.

JS Love!!!