Node.js: Tools & Skills
60 pages
English

Vous pourrez modifier la taille du texte de cet ouvrage

Découvre YouScribe en t'inscrivant gratuitement

Je m'inscris

Découvre YouScribe en t'inscrivant gratuitement

Je m'inscris
Obtenez un accès à la bibliothèque pour le consulter en ligne
En savoir plus
60 pages
English

Vous pourrez modifier la taille du texte de cet ouvrage

Obtenez un accès à la bibliothèque pour le consulter en ligne
En savoir plus

Description

While there have been quite a few attempts to get JavaScript working as a server-side language, Node.js (frequently just called Node) has been the first environment that's gained any traction. It's now used by companies such as Netflix, Uber and Paypal to power their web apps. Node allows for blazingly fast performance; thanks to its event loop model, common tasks like network connection and database I/O can be executed very quickly indeed.


In this book, we'll take a look at a selection of the related tools and skills that will make you a much more productive Node developer. It contains:


  1. Installing Multiple Versions of Node.js Using nvm
  2. A Beginner's Guide to npm
  3. Create New Express.js Apps in Minutes with Express Generator
  4. An Introduction to AdonisJs, a Laravel-like Node.js Framework
  5. Top 5 Developer-friendly Node.js API Frameworks
  6. Using MySQL with Node.js and the mysql JavaScript Client
  7. Introduction to MongoDB

Sujets

Informations

Publié par
Date de parution 24 avril 2020
Nombre de lectures 1
EAN13 9781098122843
Langue English

Informations légales : prix de location à la page 0,0598€. Cette information est donnée uniquement à titre indicatif conformément à la législation en vigueur.

Extrait

Node.js: Tools and Skills, 2nd Edition
Copyright © 2020 SitePoint Pty. Ltd.
Ebook ISBN: 978-1-925836-39-4 Product Manager: Simon Mackie Project Editor Editor: James Hibbard English Editor: Ralph Mason Cover Designer: Alex Walker
Notice of Rights
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 embodied in critical articles or reviews.
Notice of Liability
The author and publisher have made every effort to ensure the accuracy of the information herein. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors and SitePoint Pty. Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein.
Trademark Notice
Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark.

Published by SitePoint Pty. Ltd.
Level 1, 110 Johnston St, Fitzroy VIC Australia 3065 Web: www.sitepoint.com Email: books@sitepoint.com

About SitePoint
SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals. Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums. You’ll find a stack of information on JavaScript, PHP, design, and more.
About Craig Buckler
Craig is a freelance developer, author, and speaker who never shuts up about the web.
He started coding in the 1980s when applications had to squeeze into a few kilobytes of RAM. His passion for the Web was ignited in the mid 1990s when 28K modems were typical and 100KB pages were considered extravagant.
Over the past decade, Craig has written 1,200 tutorials for SitePoint as web standards evolved. Despite living in a technically wondrous future, he has never forgotten what could be achieved with modest resources.

Preface
While there have been quite a few attempts to get JavaScript working as a server-side language, Node.js (frequently just called Node) has been the first environment that's gained any traction. It's now used by companies such as Netflix, Uber and Paypal to power their web apps. Node allows for blazingly fast performance; thanks to its event loop model, common tasks like network connection and database I/O can be executed very quickly indeed.
In this book, we'll take a look at a selection of the related tools and skills that will make you a much more productive Node developer.
Who Should Read This Book?
This book is for anyone who wants to start learning server-side development with Node.js. Familiarity with JavaScript is assumed, but we don't assume any previous back-end development experience.
Conventions Used
Code Samples
Code in this book is displayed using a fixed-width font, like so:
<h1>A Perfect Summer's Day</h1><p>It was a lovely day for a walk in the park.The birds were singing and the kids were all back at school.</p>
Where existing code is required for context, rather than repeat all of it, ⋮ will be displayed:
function animate() { ⋮ new_variable = "Hello"; }
Some lines of code should be entered on one line, but we’ve had to wrap them because of page constraints. An ➥ indicates a line break that exists for formatting purposes only, and should be ignored:
URL.open("http://www.sitepoint.com/responsive-web-➥design-real-user-testing/?responsive1");
You’ll notice that we’ve used certain layout styles throughout this book to signify different types of information. Look out for the following items.
Tips, Notes, and Warnings

Hey, You!

Tips provide helpful little pointers.

Ahem, Excuse Me ...

Notes are useful asides that are related—but not critical—to the topic at hand. Think of them as extra tidbits of information.

Make Sure You Always ...

... pay attention to these important points.

Watch Out!

Warnings highlight any gotchas that are likely to trip you up along the way.
Supplementary Materials https://www.sitepoint.com/community/ are SitePoint’s forums, for help on any tricky problems. books@sitepoint.com is our email address, should you need to contact us to report a problem, or for any other reason.
Chapter 1: Installing Multiple Versions of Node.js Using nvm
by Michael Wanyoike and James Hibbard
When working with Node.js, you might encounter situations where you need to install multiple versions of the runtime.
For example, maybe you have the latest version of Node set up on your machine, yet the project you’re about to start working on requires an older version. Or maybe you’re upgrading an old Node project to a more modern version and it would be handy to be able to switch between the two while you make the transition.
Without a good tool, this would mean spending a lot of time and effort manually uninstalling and reinstalling Node versions and their global packages. Fortunately, there’s a better way!
Introducing nvm
nvm stands for Node Version Manager. As the name suggests, it helps you manage and switch between different Node versions with ease. It provides a command-line interface where you can install different versions with a single command, set a default, switch between them and much more.
OS Support
nvm supports both Linux and macOS, but that’s not to say that Windows users have to miss out. There’s a second project named nvm-windows that offers Windows users the option of easily managing Node environments. Despite the name, nvm-windows is not a clone of nvm, nor is it affiliated with it. However, the basic commands listed below (for installing, listing and switching between versions) should work for both nvm and nvm-windows.
Installation
Let’s first cover installation for Windows, macOS and Linux.
Windows
First, we need to do a little preparation: uninstall any existing versions of Node.js delete any existing Node.js installation directories (such as C:\Program Files\nodejs ) delete the existing npm install location (such as C:\Users\<user>\AppData\Roaming\npm )
After this, download and run the latest stable installer and you should be good to go!
macOS/Linux
Unlike Windows, removing previous Node and npm installations in macOS and Linux is optional. If this is something you want to do, there are plenty of good resources available online. For example, here’s how to remove Node on macOS and on Linux . And here’s how you can remove any previous npm installation you might have.
You can install nvm using cURL or Wget. On your terminal, run the following:
With cURL :
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash
Or with Wget :
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash
Note that the version number ( v0.35.2 ) will change as the project develops, so it’s worth checking the relevant section of project’s home page to find the most recent version.
This will clone the nvm repository to ~/.nvm and will make the required changes to your bash profile, so that nvm is available from anywhere in your terminal.
And that’s it! Reload (or restart) your terminal and nvm is ready to be used.
Using nvm
If installed correctly, the nvm command is available anywhere in you terminal. Let’s see how to use it to manage Node.js versions.
Install Multiple Versions of Node.js
One of the most important parts of nvm is, of course, installing different versions of Node.js. For this, nvm provides the nvm install command. You can install specific versions by running this command followed by the version you want. For example:
nvm install 12.14.1
By running the above in a terminal, nvm will install Node.js version 12.14.1 .

Running nvm use

nvm-windows users will have to run nvm use 12.14.1 after installing.
nvm follows SemVer , so if you want to install, for example, the latest 12.14 patch, you can do it by running:
nvm install 12.14
nvm will then install Node.js version 12.14.X , where X is the highest available version. At the time of writing, this is 1, so you’ll have the 12.14.1 version installed on your system.
You can see the full list of available versions by running:
nvm ls-remote
For nvm-windows, this is:
nvm ls available

Reducing Output

Listing all available Node versions produces a lot of output. Linux users might like to qpipe that to less or grep the version they’re after. For example, nvm ls-remote | less , or nvm ls-remote | grep v12 .
npm
When installing a Node.js instance, nvm will also install a compatible npm version. Each Node version might bring a different npm version, and you can run npm -v to check which one you’re currently using. Globally installed npm packages aren’t shared among different Node.js versions, as this could cause incompatibilities. Rather, they’re installed alongside the current Node version in ~/.nvm/versions/node/<version>/lib/node_modules . This has the added advantage that users won’t require sudo privileges to install global packages.
Fortunately, when installing a new Node.js version, you can reinstall the npm global packages from a specific version. For example:
nvm install v12.14.1 --reinstall-packages-from=10.18.1
By running the above, nvm will install Node.js version 12.14.1 , the corresponding npm version, and reinstall the global npm packages you had installed for the 10.18.1 version.
If you’re not sure what the latest version is, you can use the node alias:
nvm install node
This will currently pull in version 13.6.0 .
Or you can install the most recent LTS release , using:

  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents