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

RESTful Web API Design with Node.js 10

Valentin Bojinov

Condividi libro
  1. 178 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

RESTful Web API Design with Node.js 10

Valentin Bojinov

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
RESTful Web API Design with Node.js 10 è disponibile online in formato PDF/ePub?
Sì, puoi accedere a RESTful Web API Design with Node.js 10 di Valentin Bojinov in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Web Development. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2018
ISBN
9781788629140
Edizione
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...

Indice dei contenuti