Building REST APIs with ES6, Node.js, Express, and MongoDB

In this tutorial, we will use ES6 syntax to build REST APIs. we will set up Babel to work with ES6 and we’ll be building a RESTful CRUD (Create, Retrieve, Update, Delete) API with Node.js, Express, and MongoDB. We’ll use Mongoose for interacting with the MongoDB instance.

Creating the Application

Fire up your terminal and create a new folder for the application

Initialize the application with a package.json file

Install dependencies

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
body-parser: Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
mongoose is the ORM, will use to interact with MongoDB.

Install dev dependencies

nodemon: will restart the server whenever we change something in our code.
babel-preset-env: will install the presets es2015, es2016, es2017 along with some other(
babel-cli: will compile files to garget JavaScript version.

After successful installaiton, your package.json file content might look like below package.json file.

All set, now let’s define folder structure for the app, create following folders and files –

The server.js file is the main file, here in this file we are creating HTTP server and connecting to MongoDB.

Let’s define download model, which contains two fields called fileName and path.

Let’s update your downloadController.js file with the following code, the code is pretty straightforward if you have any doubts drop a comment.

Final setup define route for each controller method. Here is downloadRoutes.js file.

How to test

You can use any REST client like advacnedREST client or postman ..etc. You can also use CURL, here you go…

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

Leave a Reply

Your email address will not be published. Required fields are marked *