Download Complete RESTful Web API Design with Node js Design and implement comprehensive RESTful solutions in Node js Valentin Bojinov PDF for All Chapters
Download Complete RESTful Web API Design with Node js Design and implement comprehensive RESTful solutions in Node js Valentin Bojinov PDF for All Chapters
com
https://ebookmeta.com/product/restful-web-api-design-with-
node-js-design-and-implement-comprehensive-restful-
solutions-in-node-js-valentin-bojinov/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/distributed-systems-with-node-js-1st-
edition-thomas-hunter-ii/
ebookmeta.com
https://ebookmeta.com/product/distributed-systems-with-node-js-1st-
edition-thomas-hunter-ii-2/
ebookmeta.com
https://ebookmeta.com/product/advanced-node-js-development-1st-
edition-andrew-mead/
ebookmeta.com
https://ebookmeta.com/product/drug-use-and-misuse-9th-edition-stephen-
a-maisto/
ebookmeta.com
Nutrition for Sport and Exercise (MindTap Course List) 5th
Edition Marie Dunford
https://ebookmeta.com/product/nutrition-for-sport-and-exercise-
mindtap-course-list-5th-edition-marie-dunford/
ebookmeta.com
The Armies and Wars of the Sun King 1643 1715 Volume 3 The
Cavalry of Louis XIV 1st Edition René Chartrand
https://ebookmeta.com/product/the-armies-and-wars-of-the-sun-
king-1643-1715-volume-3-the-cavalry-of-louis-xiv-1st-edition-rene-
chartrand/
ebookmeta.com
https://ebookmeta.com/product/cases-in-international-relations-
principles-and-applications-9th-edition-donald-m-snow-professor-
emeritus/
ebookmeta.com
https://ebookmeta.com/product/architect-modern-web-applications-with-
asp-net-core-and-azure-steve-ardalis-smith/
ebookmeta.com
Temptation (Leather & Chrome #2) 1st Edition Kiki Clark
https://ebookmeta.com/product/temptation-leather-chrome-2-1st-edition-
kiki-clark/
ebookmeta.com
www.it-ebooks.info
RESTful Web API Design with
Node.js
Valentin Bojinov
BIRMINGHAM - MUMBAI
www.it-ebooks.info
RESTful Web API Design with Node.js
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78398-586-9
www.packtpub.com
www.it-ebooks.info
Credits
Reviewers Proofreaders
Abhishek Dey Maria Gould
Stefan Lapers Ameesha Green
Ângelo Marcos Rigo
Indexer
Commissioning Editor Monica Ajmera Mehta
Edward Gordon
Production Coordinator
Acquisition Editor Alwin Roy
Nikhil Karkal
Cover Work
Content Development Editor Alwin Roy
Ritika Singh
Technical Editors
Monica John
Mrunmayee Patil
Rikita Poojari
Copy Editor
Vikrant Phadke
www.it-ebooks.info
About the Author
A few years after Valentin started his career as a .NET developer, he realized that
B2B and SOA were his passion. He then moved to SAP, where he contributed to the
development of the web services stack of the SAP JEE platform. He currently works
as a senior Java developer for the Bulgarian branch of Seeburger AG, a leader in
the B2B and MFT solutions market. There, he develops and maintains several B2B
communication adapters, including web services and SAP adapters.
www.it-ebooks.info
About the Reviewers
Stefan Lapers started his career almost 20 years ago as an IT support engineer, and
quickly grew to serve Linux/Unix system engineering and software development.
www.it-ebooks.info
Ângelo Marcos Rigo is a developer with 15 years of experience, with significant
focus on web development. He always wants to learn better and new technologies.
He has worked for the prominent companies in the fields of telecommunication,
IT, education, and now, web marketing. He has also worked as a reviewer on
Moodle Security, Packt Publishing, and is available for consulting at
http://www.u4w.com.br.
I would like to thank my wife and daughter for all their support.
www.it-ebooks.info
www.PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface iii
Chapter 1: REST – What You Didn't Know 1
A brief history of REST 2
Principle 1 – everything is a resource 2
Principle 2 – each resource is identifiable by a unique identifier 3
Principle 3 – use the standard HTTP methods 3
Principle 4 – resources can have multiple representations 5
Principle 5 – communicate statelessly 6
The REST goals 7
Separation of the representation and the resource 8
Visibility 9
Reliability 10
Scalability and performance 10
Working with WADL 11
Taking advantage of the existing infrastructure 12
Summary 12
Chapter 2: Getting Started with Node.js 13
Installing Node.js 14
Node Package Manager 15
Installing the Express framework and other modules 17
Setting up a development environment 19
Handling HTTP requests 21
Modularizing code 23
Testing Node.js 26
Working with mock objects 29
Deploying an application 31
Nodejitsu 31
Microsoft Azure 32
[i]
www.it-ebooks.info
Table of Contents
Self-test questions 33
Summary 33
Chapter 3: Building a Typical Web API 35
Specifying the API 36
Implementing routes 44
Testing the API 48
Content negotiation 50
Cross-origin resource sharing 52
API versioning 53
Self-test questions 54
Summary 55
Chapter 4: Using NoSQL Databases 57
Key/value store – LevelDB 58
Document store – MongoDB 66
Database modeling with Mongoose 68
Testing a NoSQL database solution 77
Content delivery network 80
Self-test questions 81
Summary 81
Chapter 5: Implementing a Full-fledged RESTful Service 83
Extensibility and versioning 84
Working with arbitrary data 87
Linking 93
Implementing paging and filtering 95
Caching 101
Discovering and exploring RESTful services 103
Self-test questions 108
Summary 109
Chapter 6: Keeping the Bad Guys Out 111
Authentication 112
Basic authentication 112
Passport 115
Passport basic authentication strategy 116
Passport's third-party authentication strategies 118
Authorization 120
Transport Layer Security 125
Self-test questions 128
Summary 129
Index 131
[ ii ]
www.it-ebooks.info
Preface
RESTful services have become the de facto standard data feed providers for social
services, news feeds, and mobile devices. They deliver a large amount of data to
millions of users. Thus, they need to address high-availability requirements, such
as reliability and scalability. This book will show you how to utilize the Node.
js platform to implement a robust and performant data service. By the end of this
book, you will have learned how to implement a real-life RESTful service, taking
advantage of the modern NoSQL database to serve both JSON and binary content.
Important topics such as correct URI structuring and security features are also
covered, with detailed examples, showing you everything you need to know to start
implementing the robust RESTful APIs that serve content for your applications.
Chapter 2, Getting Started with Node.js, teaches you how to install Node.js and work
with its package manager to install modules. It is in this chapter that you develop
your first HTTP server application.
Chapter 3, Building a Typical Web API, takes you through structuring your application
using human-readable URL and URI parameters. You get to develop a read-only
RESTful service application using the filesystem for storage.
Chapter 4, Using NoSQL Databases, showcases how to use the LevelDB and MongoDB
NoSQL databases, and explains the difference between key-value and document
data stores.
[ iii ]
www.it-ebooks.info
Preface
Chapter 6, Keeping the Bad Guys Out, covers restricting access to your data by choosing
an appropriate authentication approach. Now you can protect data leakage with
transport layer security.
• Node.js (http://www.nodejs.org/)
• Enide developer studio or the Nodeclipse plugin for the Eclipse IDE
(http://www.nodeclipse.org/enide/studio/)
• SoapUI (http://www.soapui.org)
• OpenSSL (http://www.openssl.org)
• The Nodeclipse plugin (http://www.nodeclipse.org)
• The NPM package manager (https://www.npmjs.org/)
Conventions
In this book, you will find a number of styles of text that distinguish between
different kinds of information. Here are some examples of these styles, and an
explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"This tells npm that our package depends on the url and express modules."
[ iv ]
www.it-ebooks.info
Preface
New terms and important words are shown in bold. Words that you see on the
screen, in menus or dialog boxes for example, appear in the text like this: "Select
File | New | Node.js Project and enter the name of your first project"
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about
this book—what you liked or may have disliked. Reader feedback is important for us
to develop titles that you really get the most out of.
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to
help you to get the most from your purchase.
[v]
www.it-ebooks.info
Preface
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you find a mistake in one of our books—maybe a mistake in the text or
the code—we would be grateful if you would report this to us. By doing so, you can
save other readers from frustration and help us improve subsequent versions of this
book. If you find any errata, please report them by visiting http://www.packtpub.
com/submit-errata, selecting your book, clicking on the errata submission form link,
and entering the details of your errata. Once your errata are verified, your submission
will be accepted and the errata will be uploaded on our website, or added to any list of
existing errata, under the Errata section of that title. Any existing errata can be viewed
by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media.
At Packt, we take the protection of our copyright and licenses very seriously. If you
come across any illegal copies of our works, in any form, on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors, and our ability to bring
you valuable content.
Questions
You can contact us at questions@packtpub.com if you are having a problem with
any aspect of the book, and we will do our best to address it.
[ vi ]
www.it-ebooks.info
Other documents randomly have
different content
JUOMARIT
Renessanssi-tarina
»Et noita ole, annan autuuteni sen puolesta! Ma yksin tiedän tuon,
salaisuutes… Pikemmin marttyyri ja hurskas uhri olet, oi Aspara! Mun
tunnetko sa vielä?»
»Miksi, Martianus?»
Spara hymähtää:
»Niin, menin luostarihin!»
»Ja sinä siihen virkoit, hyvin muistan: »Myös voitko anteeks antaa
niinkuin Kristus?» Sa haastoit, niinkuin sopi Kirkon miehen, mi
häväistykselt' esivaltaansa haluaa suojata ja sieluja, karitsakatrasta
tuot' taulapäistä, min pelastus niin rihmast' ohuesta kuin pyhyydestä
Pyhän Isän riippuu! Ma tein kuin neuvoit, lähdin luostariin salaamaan
saastaa Pyhän istuimen, veisaamaan ylistystä Kirkon päälle, sen
lierolle tiara-otsaiselle, ilveelle ihanimman ihmispedon… Mut unohtaa
en tainnut…. Täällä versoi rinnassa rikos, rakkaus ja viha… Elämän
hylkimänä Kuoleman apuriks antauduin… Oh, ruumiita, kasoittain,
röykkiöittäin kaikkialla ma nään…!»
Kangertain kieli noidan käy, suu vaahdos on, ja silmät niinkuin
pätsi palavat tulta tuntemattoman yön kraatterin, viheräin huulten
viiva kuin vilun haavan lehti vipajaa.
»Niin yht' en tainnut hälle anteeks antaa: mun että hylkäsi hän
raiskattuaan. Niin rietas henkensä mun kietoi raukan, ett' unohdin,
ken oli hän ja minä, ett' unohdin, ett' armaan Benedetton viel' äsken
olin puhdas vihkivaimo. Niin hurjan lemmenliekin sytti hän poveeni
mun kuin voi vain lempo itse. Ma häntä lemmin, siks' myös vihasin,
enemmän vielä hempukoita noita, hopeisin silkkirihmoin vyötettyjä,
joill' oli iltajuhliss' isoisissa tuon kauniin paholaisen luokse pääsy.
Aqua di Napolin ma keitin heille, paaville pyhälle taas pyhän
mannan, vihan ja lemmen myrkyll' lääkityn. En anteeks antaa
voinut… enkä ano ma anteeks-antamusta itsellein, vain kuolla voin
ma… samaten kuin itse ma tuottanut oon muille kuolemaa…»
»Mua ällös sure, rippi-isä kallis, nyt olen onnellinen… Minut noutaa
nyt morsiokseen hän, jot' enin lemmin elossa… Kuolema… Hyvästi
jää.»
»Et saa, et saa sa kuolla noin, oi, onneton Aspara! Ma yksin olen
syyhys vikapää, ma yksin olen rietas, valapatto, himojen lokaviemär,
myrkyn syömä. Sa syntis tunnustit, sait synninpäästön. Mut kuka
antaa enää voi sen mulle, kun kuolit sinä, jota vastaan rikoin, ma
ulkokultainen ja sala-irstas! Äh, sua lempinyt ei Benedetto, tuo liukas
kelmi-keikar, luutunlyöjä, ei lemmensairas pyhä pyövelisi, mi kuolon
enkeliks sun hahmos muutti, vaan minä, minä yksin sua lemmin! Sen
vuoks sun kahlehdin ma nunnan kaapuun, eläimen taljoiss' itse
lymysin syvimmän korven kohduss', suulla tulppa, ma etten haastais
sopimattomia himosta synnillisen sydämeni!
***
Keskiaikainen ballaadi
Pyhä Perth noin virkki: »En miekan mies ole konsana ollut, sen
Jumala ties, mut vannoa voin: jos Signhildin saan, hänen vuokseen
ma valloitan pakanamaan!»
Intialainen unelma
***
Talapya:
Vardhamana:
Talapya:
Vardhamana:
Talapya:
Vardhamana:
Talapya:
Vardhamana:
***
Lyö lumilaine halki kevätsään. Ol' ennen onni elää turhan vuoksi ja
tyhjää kurkoittaa, nyt ikävään ja yrmyyn saa mun ilma, maa ja taivas
ja yö ja päivä, eljet veen ja jään, tää elon terhenleikki aina sama, tää
sielun kaipuu irti itsestään, tää uni ikuisemman rauentama.
JÄINEN MAA
Miks, Luoja, luonut olet mun aineeseen? sit' enin imehdin. Kas,
yönä tällaisena täytyis voida veess', ilmass', eetterissä karkeloida.
Tuolt' tahtoisin ylhält' Uranialta unten solet, täht'onnen seppeleen!
MYRSKYLINNUT