Node Toolchain for Newbies: More Libraries for Node.js Projects
Articles,  Blog

Node Toolchain for Newbies: More Libraries for Node.js Projects

The next library is the go-to library
for querying your… HTML, HTML data! Let’s say you’re building a web crawler. What
is the web crawler or web scraper? They are certain scripts, there are certain
applications that go to URLs and then they, they interpret that data, they
store it, they index it, put it in the database. They do something with that data. So you
can use the jQuery syntax the good old familiar jQuery from the front end in
your back-end to get to some element or maybe you’re performing some testing. For
the testing on the back end it’s nice to use this jQuery syntax you want to get
some element, you want to compare what a label of a certain element, what are the
text inside of that what is the structure, what are some attributes.
Cheerio is the way to go! Node-oauth. So node-oauth, node-oauth is my favorite library and it’s
very mature, it’s tested by myriads and myriads of applications. I do not
recommend using passport, or everyoauth or any one of those fancy oauth integration
libraries. Why? Because oftentimes they don’t do what you need. It’s better to
just use node-oauth. What it gives you, it gives you a few methods: how to create a
signature you can put it into an HTTP header, and then it also gives you
requests, if you want to do those if you want to use this you can use those or
you can create your own request after getting that signature for auth. It
supports oauth one and oauth two as well. You can implement three-legged
authorization authentication or any other strategy. Passport. So passwords is
one of those bigger libraries. I do not recommend using it but if it works for
you that’s fine. What is the difference between passport and node-oauth. So with
passport you basically install passport and you install a strategy, for example
Facebook or Twitter strategy, or Google strategy for Google API, and you put your
API keys and then you configure the passport to work with Express or any
other framework and that’s it. In theory it should work. I oftentimes have more
problems with passport but if it works for you, a lot of developers they prefer
to use passport and that’s great too. My job is to give you this tool, and so you
have the full toolchain. yargs, so yeah it’s a nice module to work with command
line arguments. So let’s say you building a command line script that
will do some deployment or it will do something else, maybe you’re building
your own version of webpack which is precisely tailored to your
application so yargs, that’s the library to work with those command line
arguments to access them to have a beautiful help menu, to basically have
that support easier, because process.argv it’s just low level access and
it’s not that nice to work with. Shelljs Shelljs, that’s a tool or
library, so it’s a library to build scripts to run shell scripts, typically
bash scripts and it will give you a nice methods but it’s also cross-platform, so
it will work at least in theory, I’m not a Windows user, and it will work on
Windows. So instead of you calling child child_process.exec with all those bash
commands use shelljs: to create a folder, to run some commands, to delete a file to.
All those commands they are supported in this library so from within your node
code you can execute this shell commands. Mocha. Mocha is my favorite and it’s the
most popular testing framework. It’s full of features, it’s full of reports, the
language is nice. Language is compatible with Jasmine and Jest, two other popular
choices, good choices. Mocha is the testing framework, it’s the testing framework of
nodejs. Async. Async, it’s my favorite library
work with asynchronous code. Every time I need to run a lot of functions
concurrently or sequentially I would use async. Async will allow me to create an
array and then I just pass that array and I have a callback for the final
execution. So async works nicely. There are a couple other libraries alternative
to async, so use one of them. One is called neo-async another called nasync (steed is better) use async or some of those alternatives but definitely, definitely it’s a good way to run
your code. Promises they might have, especially promise.all, it can have
some of the functionality of async but still async has so much to give you in
any many many medium-sized or large- sized projects async that’s a way to go. Concurrently. Concurrently, it’s now becoming my favorite tool. Why? Because
when I’m building front-end application and the backend application in one
repository, so I have a server and my front-end code I would need to start webpack, maybe webpack development server or node-static and then I would also need
to start the API or even with just node-static node-static and webpack, so that’s two
commands and they need to run concurrently and then with the API
server that’s a third command. Why webpack webpack needs to run concurrently?
Because I’m developing so I would launch webpack webback in the watch mode for it to
watch all the file changes. So that’s why concurrently it’s a great tool. I can
just install it and then in my in npm scripts I can tell concurrently “hey, run
those two or three processes at the same time”, and I will just do NPM start and
they’re all running in parallel, they’re running concurrently. You you can
also use it this tool from within your node code, so it also works as the
dependency not just as the command-line tool. So let’s take a look at the mocha
example. Mocha Express test, so I’m just specifying the file “mocha express.test.js”. Typically the test files they would have
a word test in the file name. And you can see I have a lot of errors, so this is
how Mocha can run in your testing. This is an example of integrational
testing so I’m testing my RESTful API by making requests to it over HTTP. I’m not
mocking it, I’m not importing my Express app and then faking those requests.
Those are real requests and then you can see in the second tab MongoDB, those are
the database tries to talk with MongoDB. Okay, so Mocha is the main, the most
popular testing library. Some of the libraries listed such as webpack and
mocha they can be installed globally okay. So how do you so globally?
It’s “-g” but I I believe it’s an anti-pattern because the local installation has so
many benefits over the global. Yes, you are taking up more space but the space
is so cheap right now, we don’t really worry about that with the local
installation you can run multiple versions of the same tool for different
projects, all you have to do is just specify it and then specify it in
package.json, and then if someone wants to use the same project,
collaborate on your project they don’t need to worry that they have a different
global tool. The tool will be in package.json and it will be so locally once
they do npm install, so they will have zero conflict. Of course there are many,
many, many alternatives for example for Axios there are request and super-agent.
They are super popular with HTTP libraries but I’m giving you the latest
the most modern options and the most popular as well, so it’s either the most
popular maybe it’s a little bit older like mocha. Jest, it’s definitely newer
and it has certain advantage for the react code for the react component or
they’re newer and they support wonderful things such as promises and async/await.
This is the case for Axios, request and superagent. They’ve been around for a
while, there are more projects with them but Axios is the way to go. So obviously
I cannot list all of them and I don’t want to overwhelm you. I would give you
the most bang for your time invested in this course, so I’m giving you the best
tools. And then go ahead an experiment. Use something else, use alternative but first
get this toolchain, get this toolchain under your belt so you have the best
tool for the job whatever that job is, whether it’s making HTTP requests or
creating tests.

Leave a Reply

Your email address will not be published. Required fields are marked *