Create REST API with Node.js for mobile programmer (part 1)

TuyenBQ
3 min readNov 9, 2016

--

Today is a history with American people, Donald Trumps has just win an election battle and will be president next 45th of USA. Congrats Trumps and his team. Enough, will be back to this tutorial.

To publish a great mobile application, behind that, must to have a great backend. Nowadays, with a separate mobile programmer, keeping develop mobile app and service backend are two tasks but synchronous. But building a REST API for your app could be a bit of complex and hard thing if you have not been done before. Overcome that fear, this tutorial will help you build a tiny REST API using Node.js

I choose Node.js for:

  • It’s easy to work with JSON in JavaScript, because JSON stands for JavaScript Object Notation!
  • Node.js is lightweight and easy to get started with.
  • Node.js gives you fine-grained control over your request and responses.

Node.js and Express

Today i will show a demo to take a GET request to response list of most famous football players in the world. Format is:

GET / footballPlayers

The JSON object takes the form of:

We’ll also be using the following tools to build and test this backend:

  • Node.js — the runtime for our app.
  • Express.js — a popular, lightweight framework for building Node.js apps
  • Postman — a HTTP client that allows us to make custom requests to the REST API

Starting Your Node.js Mobile App

To get started, make sure that you:

  1. Install a great text editor. I like Sublime, Atom, or VS Code
  2. Install the Node.js runtime. You can install the “Current” (v6) version of Node.
  3. Install Postman

Setting Up Your App

To get started with your Node.js project, we’ll use npm. npm is a package manager for JavaScript projects, and allows you to install JavaScript tools and modules for your project. It’s automatically included in your Node.js install, so all we have to do is start using it in the command line!

To get started, create a folder, navigate to it in the command line, and run:

$ npm init

npm will ask you a lot of questions — feel free to leave them as the default!

Once you’re done, npm will create a package.json file in your folder. package.json keeps track of your project information and dependencies!

In addition, we need to install express, a popular, minimalist web framework for Node.js. Install it by running this command:

$ npm install --save express

Express folder went done looks like this.

With this command, npm will install express, and save its version to the package.json file. Now, we can use express in our project.

Code Your First REST API

We’re all set with the project, so let’s start writing our REST API! Create a file named mainTest.js, and type in the following:

Simple, right? In 17 lines of code, we:

  • Imported the Express module
  • Initialized the Express object
  • Added a handler for GET /footballPlayers which responds to the request with a JSON object with sample football players data
  • Told express to listen to HTTP requests on port 3000

Try running it in the command line:

node mainTest.js

Congrats, you now have a basic REST API with one endpoint, and it’s running on your machine! Try visiting it at http://localhost:3000/footballPlayers and see what happens.

The results

To next blog, i will show you the way to connect your REST API with persistent store data online likes MongoDB, MySQL or something like that by Node.js and return to your mobile location app.

It’s enough today. I hope you’ve enjoyed this blog. If you have any questions, comments or feedback please leave it below.

Peace ✌

--

--