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

RESTful Web API Design with Node.js 10

Valentin Bojinov

Compartir libro
  1. 178 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

RESTful Web API Design with Node.js 10

Valentin Bojinov

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es RESTful Web API Design with Node.js 10 un PDF/ePUB en línea?
Sí, puedes acceder a RESTful Web API Design with Node.js 10 de Valentin Bojinov en formato PDF o ePUB, así como a otros libros populares de Computer Science y Web Development. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788629140
Edición
3
Categoría
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...

Índice