RESTful Web API Design with Node.js 10
eBook - ePub

RESTful Web API Design with Node.js 10

Valentin Bojinov

Share book
  1. 178 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

RESTful Web API Design with Node.js 10

Valentin Bojinov

Book details
Book preview
Table of contents
Citations

About This Book

Design and implement scalable and maintainable RESTful solutions with Node.js 10About This Bookβ€’ Create rich and scalable RESTful API solutions from scratchβ€’ Explore the new features of Node.js 10, Express 4.0, and MongoDBβ€’ Integrate MongoDB in your Node.js application to store and secure your dataWho This Book Is ForIf you are a web developer keen to enrich your development skills to create server-side RESTful applications based on the Node.js platform, this book is for you. Some knowledge of REST would be an added advantage, but is definitely not a necessity.What You Will Learnβ€’ Install, develop, and test your own Node.js user modulesβ€’ Understand the differences between HTTP and RESTful applicationsβ€’ Use self-descriptive URLs and set accurate HTTP status codesβ€’ Eliminate third-party dependencies in your tests with mockingβ€’ Implement automation tests for a REST-enabled endpoint with Mochaβ€’ Secure your services with NoSQL database integration within Node.js applicationsβ€’ Integrate a simple frontend using JavaScript libraries available on a CDN serverIn DetailWhen building RESTful services, it is really important to choose the right framework. Node.js, with its asynchronous, event-driven architecture, is exactly the right choice for building RESTful APIs.This third edition of RESTful Web API Design with Node.js 10 will teach you to create scalable and rich RESTful applications based on the Node.js platform. You will be introduced to the latest NPM package handler and understand how to use it to customize your RESTful development process. You will begin by understanding the key principle that makes an HTTP application a RESTful-enabled application. After writing a simple HTTP request handler, you will create and test Node.js modules using automated tests and mock objects; explore using the NoSQL database, MongoDB, to store data; and get to grips with using self-descriptive URLs. You'll learn to set accurate HTTP status codes along with understanding how to keep your applications backward-compatible. Also, while implementing a full-fledged RESTful service, you will use Swagger to document the API and implement automation tests for a REST-enabled endpoint with Mocha. Lastly, you will explore some authentication techniques to secure your application.Style and approachStep-by-step practical guide to building RESTful applications

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on β€œCancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is RESTful Web API Design with Node.js 10 an online PDF/ePUB?
Yes, you can access RESTful Web API Design with Node.js 10 by Valentin Bojinov in PDF and/or ePUB format, as well as other popular books in Computer Science & Web Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788629140
Edition
3

Getting Started with Node.js

In this chapter, you will gain your first real Node.js experience. We will start by installing Node.js, along with some modules we will use throughout this book. Then, we will set up a development environment. Throughout the book, the Atom IDE will be used. Yes, GitHub's online editor has finally landed for desktop environment and is available on a platform of your preference!
Next, we will create a workspace and start developing our first Node.js application. It will be a simple server application processing incoming HTTP requests. We will go one step further, demonstrating how to modularize and unit test our JavaScript code. Finally, we will deploy our first application on the Heroku Cloud Application platform.
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

Let's start our journey through Node.js with a Node.js installation. Installers are available for both Windows and macOS at https://nodejs.org/en/download/. At the time of writing, Node.js 10 has just been released as the current version, and is about to become the next Long Term Support version, in August 2018. Linux users can either build Node.js from the available Linux binaries or make use of their package manager, as Node.js is available with most of the popular package repositories for different Linux distributions. For instance, Ubuntu and other Debian-based distributions should first point to the latest Node.js 10 package and then install via the apt-get command from the shell:
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install nodejs
If you decide to go with an installer available for macOS or Windows, a wizard will guide you through a rather typical installation procedure, where you will have to accept the Node.js license agreement and then provide an installation path.
Linux users performing installations via package managers will need to install Node Package Manager (npm) separately; we will do that in the next section.
After a successful installation, you should have Node set on your PATH environment variable.
The installer will have preselected for you the Node.js runtime, npm, shortcuts to online documentation resources, as well as the option to add Node.js and npm to your operating system PATH environment variable.
To verify that your installation was successful, execute the following from your shell:
node --version
At the time of writing, the latest Node.js version is 10.0.0 so, as expected, this version number will be the output of the version check. Node.js 10 will be the next Long Term Supported version, so it will stay actual for the next few years

Npm

Node.js eases support to third-party open source-developed modules by providing npm. It allows you, as a developer, to easily install, manage, and even provide your own module packages. The npm package repository 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
If you upgraded your Node.js installation and you had previously installed npm 5.6, you would be asked to upgrade it to version 6. To do that, just execute:
sudo npm i -g npm
Once npm is installed, it is useful 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 currently-installed Node.js modules:
bojinov@developer-machine:~$ npm ls
/home/bojinov
β”œβ”€β”¬ [email protected]
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ └── [email protected]
β”‚ └── [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”¬ [email protected]
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ β”œβ”€β”€ [email protected] deduped
β”‚ β”‚ β”œβ”€β”€ [email protected]
β”‚ β”‚ └── [email protected] deduped
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ └── [email protected]
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ β”œβ”€β”€ [email protected]
β”‚ β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ β”‚ └── [email protected]
β”‚ β”‚ └─┬ [email protected]
β”‚ β”‚ └── [email protected]
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ └── [email protected] extraneous
β”‚ β”œβ”€β”€ [email protected]
β”‚ β”œβ”€β”€ [email protected]
β”‚ └── [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]

Installing the Express framework and other modules

Now that we have npm installed, let's make use of it and install some of the modules we will be using heavily throughout this book. The most important among them is the Express framework (http://www.expressjs.com/). It is a flexible web application framework for Node.js, providing a robust RESTful API for developing single or multi-page web applications. The following command will download the Express module from the npm repository and make it available for our local Node.js installation:
npm install -g express 
You will find the express module among the results of an npm ls after a successful installation. Later in this chapter, we will learn how to write unit tests for our Node.js modules. We will need the nodeunit module for that purpose:
npm install nodeunit -g 
The -g option will install nodeunit globally. This means that the module will be stored at a central place on your filesystem; usually, that is either /usr/lib/node_modules or /usr/lib/node, but that can be configured to the global configuration of your Node.js. Globally installed modules are available to all running node applications.
Locally installed modules will be stored in a node_modules subdirectory of the current working directory of your project and will be available only to that single project.
Now, coming back to the nodeunit moduleβ€”it provides basic assert test functions for creating basic unit tests as well as tools for executing them.
Before starting to develop with Node.js, we have one more thing to look into: the package descriptor file of a Node.js application.
All Node.js applications or modules contain a package.json descriptor file. It provides meta-information about the module, its authors, and the dependencies it uses. Let's take a look at the package.json file of the express module we installed earlier:
{
"_from": "express",
"_id": "[email protected]",
"_inBundle": false,
"_integrity": "sha512-STB7LZ4N0L+81FJHGla2oboUHTk4PaN1RsOkoRh9OSeEKylvF5hwKYVX1xCLFaCT7MD0BNG/gX2WFMLqY6EMBw==",
"_location": "/express",
"_phantomChildren": {},
"_requested": {
"type": "tag", "registry": true, "raw": "express", "name": "express",
"escapedName": "express","rawSpec": "", "saveSpec": null, "fetchSpec": "latest"
},
"_requiredBy": [
"#USER"
],
"_resolved": "https://registry.npmjs.org/express/-/express-4.16.1.tgz",
"_shasum": "6b33b560183c9b253b7b62144df33a4654ac9ed0",
"_spec": "express",
"_where": "/home/valio/Downloads",
"author": {
"name": "TJ Holowaychuk",
"email": "[email protected]"
},
"bugs": {
"url": "https://github.com/expressjs/express/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Aaron Heckmann",
"email": "[email protected]"
},
...,
{
"name": "Young Jae Sim",
"email": "[email protected]"
}
],
"dependencies": {
"accepts": "~1.3.4",
"array-flatten": "1.1.1",
"body-parser": "1.18.2",
...,
"type-is": "~1.6.15",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
},
"deprecated": false,
"description": "Fast, unopinionated, minimalist web framework",
"devDependencies": {
"after": "0.8.2",
"connect-redis": "~2.4.1",
...,
"should": "13.1.0",
"supertest": "1.2.0",
"vhost": "~3.0.2"
},
"engines": {
"node": ">= 0.10.0"
},
"files": ["LICENSE", "History.md", "Readme.md", "index.js","lib/"],
"homepage": "http://expressjs.com/",
"keywords": [
"express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api"
],
"license": "MIT",
"name": "express",
"repository": {
"type": "git",
"url": "git+https://github.com/expressjs/express.git"
},
"scripts": {
"lint": "eslint .",
"test": "moc...

Table of contents