Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
RESTful  Web API Design with Node.js
RESTful  Web API Design with Node.js

RESTful Web API Design with Node.js: A step-by-step guide in the RESTful world of Node.js. , Second Edition

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

RESTful Web API Design with Node.js

Chapter 2.  Getting Started with Node.js

In this chapter, you will gain your first real experience with Node.js. We will start by installing Node.js, along with some of the modules we will use throughout this book. Then, we will set up a development environment. I have chosen the Nodeclipse plugin for the well-known Eclipse IDE, which comes preinstalled within the Enide Developer Studio.

Next, we will create a workspace and start developing our first Node.js application. It will be a simple server processing incoming HTTP requests. Then we will go one step further, demonstrating how to modularize and test our JavaScript code. Finally, we will take a look at some cloud environment offerings that we can use to deploy our applications.

To sum up, in this chapter, we will cover the following topics:

  • Installing Node.js
  • Installing the Express framework and other modules
  • Setting up a development environment
  • Handling HTTP requests
  • Modularizing code
  • Testing Node.js
  • Deploying an application...

Installing Node.js

The current version of the latest Node.js release is five. This is a stable short-term support release, which should serve as a bridge between the latest Long Term Support and the following one.

Let's start our journey through Node.js with a Node.js installation. Installers are available both for Windows and Mac operating systems at http://www.nodejs.org/download/. Linux users can either build Node.js from the available Linux binaries or can make use of their package manager, as Node.js is available with most of the popular package repositories for different Linux distributions. Currently, there are two versions available in the repositories — €”the LTS Version 4.x and the stable 5.x. By default, the package manager points to the LTS release, so we should explicitly tell the manager to install the 5.x Version via the following command:

curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
apt-get install nodejs

For now, let's install...

Node Package Manager

Node.js eases support to third-party open source developed modules by providing the Node Package Manager (we've already mentioned it, that is, NPM). It allows you, as a developer, to easily install, manage, and even provide your own modules for a rapidly grown and well-maintained open source repository. It is available at http://www.npmjs.org/ and is accessible via its command-line interface.

If you didn't use the installer, then you will need to install NPM separately. For example, Ubuntu users can make use of their package installer as follows:

apt-get npm install

Once NPM is installed, you may want to set it permanently in your user profile's PATH environment variable by editing the ~/.profile file to export the path to NPM as follows:

export PATH=$PATH:/path/to/npm

After a successful NPM installation, use NPM's  ls option to display the already installed Node.js modules:

Node Package Manager

Installing the Express framework and other modules

Now that we have NPM...

Deploying an application

Node.js has an event-driven, non-blocking I/O model, which makes it perfect for real-time applications that scale well in distributed environments, such as public or private cloud platforms. Each cloud platform offers tools that allow seamless deployment, distribution, and scaling of its hosted applications. In this section, we will look at two publicly available Node.js application cloud providers — €”Nodejitsu and Microsoft Azure.

But first, let's spend some time on the clustering support, as it is fundamental for understanding why Node.js fits so well in the cloud environment. Node.js comes with clustering support built in its core. Using the cluster module in your applications allows them to start as many workers as necessary to handle the load they will face. Generally, it is recommended to match the number of workers to the number of threads or logical cores your environment has.

Note

The heart of your application is the master process...

Microsoft Azure

Microsoft's cloud platform as a service, Azure, also offers hosting of Node.js applications. They have chosen a slightly different approach, and instead of providing a command-line interface to interact with their repositories, they make use of their Git integration; that is, you interact with Azure as you would interact with any other Git repository. If you are not familiar with Git, I strongly recommend that you learn more about this distributed source code version control system.

Tip

If you've chosen Azure as your platform, you will find this link very useful: http://azure.microsoft.com/en-us/develop/nodejs/

Nodejitsu

Let's take a closer look at some Node.js's Platform as a Service (PaaS) offerings. The first PaaS we will look at is Nodejitsu, available at https://www.nodejitsu.com.

This allows seamless deployment of Node.js applications on the cloud, with many useful features for development, management, deployment, and monitoring of Node.js applications...

Self-test questions

To get additional confidence about your newly gained knowledge, go through the next set of statements and state whether they are true or false:

  • Node modules can export more than one function to outer components.
  • Node modules are extendable.
  • Modules always need to explicitly declare their dependencies to other modules.
  • When using mocking in a test environment, the mocked method is called on the mocked object.
  • Debugging Node.js code is not as straightforward as other pieces of non-JavaScript code.

Installing Node.js


The current version of the latest Node.js release is five. This is a stable short-term support release, which should serve as a bridge between the latest Long Term Support and the following one.

Let's start our journey through Node.js with a Node.js installation. Installers are available both for Windows and Mac operating systems at http://www.nodejs.org/download/. Linux users can either build Node.js from the available Linux binaries or can make use of their package manager, as Node.js is available with most of the popular package repositories for different Linux distributions. Currently, there are two versions available in the repositories — €”the LTS Version 4.x and the stable 5.x. By default, the package manager points to the LTS release, so we should explicitly tell the manager to install the 5.x Version via the following command:

curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
apt-get install nodejs

For now, let's install Node.js from its installer...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Create a fully featured RESTful API solution from scratch.
  • • Learn how to leverage Node.JS, Express, MongoDB and NoSQL datastores to give an extra edge to your REST API design.
  • • Use this practical guide to integrate MongoDB in your Node.js application.

Description

In this era of cloud computing, every data provisioning solution is built in a scalable and fail-safe way. Thus, when building RESTful services, the right choice for the underlying platform is vital. Node.js, with its asynchronous, event-driven architecture, is exactly the right choice to build RESTful APIs. This book will help you enrich your development skills to create scalable, server-side, RESTful applications based on the Node.js platform. Starting with the fundamentals of REST, you will understand why RESTful web services are better data provisioning solution than other technologies. You will start setting up a development environment by installing Node.js, Express.js, and other modules. Next, you will write a simple HTTP request handler and create and test Node.js modules using automated tests and mock objects. You will then have to choose the most appropriate data storage type, having options between a key/value or document data store, and also you will implement automated tests for it. This module will evolve chapter by chapter until it turns into a full-fledged and secure Restful service.

Who is this book for?

The ideal target audience for this book is web developers who have some experience with RESTful services. Familiarity with basic JavaScript programming techniques is required. No prior experience with Node.JS or Express.js is required.

What you will learn

  • • Install, develop, and test your own Node.js user modules
  • • Comprehend the differences between an HTTP and a RESTful application
  • • Optimize RESTful service URI routing with best practices
  • • Eliminate third-party dependencies in your tests with mocking
  • • Learn about NoSQL data stores and integrate MongoDB in your Node.js application with Mongoose
  • • Secure your services with NoSQL database integration within Node.js applications
  • • Enrich your development skills to create scalable, server-side, RESTful applications
  • based on the Node.js platform

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 26, 2016
Length: 148 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786469137
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : May 26, 2016
Length: 148 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786469137
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Node.js  Design Patterns
€41.99
Node.js Web Development
€36.99
RESTful  Web API Design with Node.js
€24.99
Total 103.97 Stars icon

Table of Contents

6 Chapters
1. REST – What You Did Not Know Chevron down icon Chevron up icon
2. Getting Started with Node.js Chevron down icon Chevron up icon
3. Building a Typical Web API Chevron down icon Chevron up icon
4. Using NoSQL Databases Chevron down icon Chevron up icon
5. Implementing a Fully-Fledged RESTful Service Chevron down icon Chevron up icon
6. Keeping the Bad Guys Out Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(13 Ratings)
5 star 53.8%
4 star 23.1%
3 star 7.7%
2 star 7.7%
1 star 7.7%
Filter icon Filter
Top Reviews

Filter reviews by




Hüseyin BABAL May 31, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is good for anyone who want to learn the basics of REST API Fundamentals, and developing RESTful API by using Node.js and related technologies. You can easily understand the flow of developing scalable applications by using Node.js, store your data into NoSQL databases, stabilise your applications by using TDD by using this book. Happy coding!
Amazon Verified review Amazon
Steve T. Jun 14, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I recently obtained a copy of this book and I'm impressed by the style, level of detail and usefulness of the examples. The book covers on all key concepts of REST and will walk you through the entire development process including storing the data, security, writing tests and deployment!I highly recommend this book to anybody looking at developing RESTful APIs using Node.js!
Amazon Verified review Amazon
Amazon Customer Feb 15, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is an awesome book for any developer who wants to develop software using great practices. This book explains things well and dives deep into why certain approaches are taken. Good read!
Amazon Verified review Amazon
DSJ Sep 15, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great Book!
Amazon Verified review Amazon
Gregori Jun 13, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I did like the first edition of this book as it helped me quickly dive into the topic, however, it had few weak points. I am really glad those are polished in the second edition, and I really found the automated tests for the database layer really helpful! I recommend this to anyone who needs to go into the topic quickly
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.