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

RESTful Web API Design with Node.js 10

Valentin Bojinov

Partager le livre
  1. 178 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

RESTful Web API Design with Node.js 10

Valentin Bojinov

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que RESTful Web API Design with Node.js 10 est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  RESTful Web API Design with Node.js 10 par Valentin Bojinov en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Web Development. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788629140
Édition
3
Sous-sujet
Web Development

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 des matiĂšres