Creating a REST API with Node.js (Getting Started)

Posted by aut0maat10 on June 20, 2018

During the past few weeks, I have been focusing on learning more about full stack JavaScript. I’m interested in the MERN stack (MongoDB, Express, React, Node.js), and as a company I’m interested in working for is using this particular stack, I decided to dive into some tutorials and create a RESTful API with Node.js.

Since the MERN stack is very popular, there are some great tutorials out there. I can highly recommend Creating a REST API with Node.js and An Easy Way to Get Started with the MERN Stack by alligator.io , for instance.

What is a RESTful API?

REST stands for REpresentational State Transfer. It’s an architectural style that defines a set of constraints and properties based on HTTP. It was first presented by Roy Fielding in 2000 in his famous dissertation.

REST has six architectural constraints, which you can read more about here.

Getting Started

I have Node installed, so I created a directory node-restful-shop , cd into it, and ran npm init to create a package.json file. Next, I installed Express with the command npm install --save express.

Next, I created a simple server.js file, in order to set up my Node server.

After that, I added an app.js file and hooked it up with the bare-bones server.

After starting the server with node server.js, I opened up localhost:3000 in the browser, just to check that everything works:

Now that we have a very basic server running, it’s time to add more routes, a database, and more. That, however, I’m going to save for my next blog post.