JavaScript Everywhere
Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. -Wikipedia
Node is a runtime environment for JavaScript that runs on the server instead of the client.
It's built on top of Google's V8 JavaScript engine, which is also used to run JavaScript inside of Google Chrome.
You can run it on Mac, Windows, Linux and there are even some experimental projects to run Node on other platforms like Android and iOS.
Why, oh why, would you want JS everywhere?
I wont lie, JavaScript used to be awful.
I'm happy to report that's not the case anymore!
ES2015 was the turning point
ES2015 added:
- Classes
- Block scoped variables (
let
&const
) - The ability to conveniently split code apart into separate
import
able files and the ability toexport
specific things from a file.
Benefits
Speed
Node is fast.
Depending on how you measure, it can be twice as fast as compiled languages like C, C# and Java and orders of magnitude faster than interpreted languages like Python and Ruby.
Simplicity
Run this in git bash.
echo "console.log('hello world');" > hello.js && node hello.js
There's your classic hello world!
It's just JavaScript
JavaScript is basically your only choice for client side scripting inside a web browser, so lots of people already know the language.
It's an easy tool for most teams to pick up.
Asynchronous and Simple
In most traditional languages all instructions are blocking unless you write code that allows things to run on another thread.
Node uses a single-thread and callback functions to simplify asynchronous code without blocking anything.
Because of this approach, a Node.JS web server can handle thousands of concurrent connections without the need to introduce code that maintains thread concurrency.
Blocking is the exception with Node
So. Many. Packages.
The npm
registry contains over 800,000 libraries that you can pull into your project by simply running a command like yarn add express
at the command line.
Here's a fully functional "Hello world!" web server using the aforementioned express
package:
const express = require("express");
const app = express();
app.get("/", function(req, res) {
res.send("Hello World");
});
app.listen(3000);
Bonus: Nodemon
Nodemon (pronounced like Pokémon) is an amazing package that essentially sits on top of node and refreshes your project for you anytime you make a change. So instead of making changes to your application, shutting down the current server instance, and then starting a new one, Nodemon simply watches for changes in the file system and goes through that process once it detects one.
Installing Nodemon
You can install Nodemon globally on your machine so that it's available everywhere, but I prefer to save it as a local dependency in every project. That way, if someone else clones my project and wants to work on it, I don't have to make any assumptions about their machine because I know all of the necessary dependencies are installed locally. So, we can add Nodemon to a project like so:
$ npm i nodemon
Once it's installed we can use it like this:
$ nodemon index.js # Instead of using `node index.js`
Now Nodemon will watch for any changes in any connected files and update the application when they happen! Such a time saver.
In Closing
JavaScript is an extremely popular language that has grown leaps and bounds over the last 4 years. Additionally, the Node eco-system includes the largest package repository in the world with over 800,000 packages hosted on NPM.
By using the Node run-time, we are no longer limited to using JavaScript inside the web browser. Node is a solid choice for lots of tasks, ranging from web servers to build automation and of course client-side coding.
We can run JS everywhere. It's fun. It's productive, and it's rapidly evolving.