SitePoint
  • Blog
  • Discord
  • Forum
  • Library
  • Login
Join Premium

Build Your First Node App

Close
  1. Preface
    • Build Your First Node App
    • Notice of Rights
    • Notice of Liability
    • Trademark Notice
    • About SitePoint
    • Conventions Used
  2. 1What Is Node.js?
    • How Do I Install Node.js?
  3. What Is Node.js Used For?
    • Node.js Lets Us Run JavaScript on the Server
  4. Laying the Foundations of Our App
    • What is MVC?
    • Laying out the Foundation
    • Storing Our Settings
  5. Defining the Routes
  6. Building the Models
    • Setting up the database
    • Creating our Note model
  7. Synchronizing the Database
    • Building the controllers
  8. Boilerplate of the Note Controller
    • The “create” function
    • The “read” function
    • The “update” function
    • The “delete” function
    • Using the Note controller in our routes
  9. Building the Views
    • The note component
    • The base layout
    • The home view
    • The note view
    • The JavaScript on the client
  10. Adding Support for Views on the Server, and Serving Static Files
    • Setting the home view
    • Setting the note view: create method
    • Serving Static Files
    • You did it!

You don't have any bookmarks for this book yet.

What Is Node.js?

There are plenty of definitions to be found online. Let’s take a look at a couple of the more popular ones:

This is what the project’s home page has to say:

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

And this is what StackOverflow has to offer:

Node.js is an event-based, non-blocking, asynchronous I/O framework that uses Google’s V8 JavaScript engine and libuv library.

Hmmm, “non-blocking I/O”, “event-driven”, “asynchronous” — that’s quite a lot to digest in one go. So let’s approach this from a different angle and begin by focusing on the other detail that both descriptions mention — the V8 JavaScript engine.

Node Is Built on Google Chrome’s V8 JavaScript Engine

The V8 engine is the open-source JavaScript engine that runs in the Chrome, Opera and Vivaldi browsers. It was designed with performance in mind and is responsible for compiling JavaScript directly to native machine code that your computer can execute.

However, when we say that Node is built on the V8 engine, we don’t mean that Node programs are executed in a browser. They aren’t. Rather, the creator of Node (Ryan Dahl) took the V8 engine and enhanced it with various features, such as a file system API, an HTTP library, and a number of operating system–related utility methods.

This means that Node.js is a program we can use to execute JavaScript on our computers. In other words, it’s a JavaScript runtime.

How Do I Install Node.js?

In this next section, we’ll install Node and say hello (world). We’ll also look at npm, a package manager that comes bundled with Node.

Node Binaries vs Version Manager

Many websites will recommend that you head to the official Node download page and grab the Node binaries for your system. While that works, I would suggest that you use a version manager instead. This is a program which allows you to install multiple versions of Node and switch between them at will. There are various advantages to using a version manager. For example, it negates potential permission issues which would otherwise see you installing packages with admin permissions.

If you fancy going the version manager route, please consult our quick tip: Install Multiple Versions of Node.js using nvm. Otherwise, grab the correct binaries for your system from the link above and install those.

“Hello, World!” the Node.js Way

You can check that Node is installed on your system by opening a terminal and typing node -v. If all has gone well, you should see something like v8.9.4 displayed. This is the current LTS version at the time of writing.

Next, create a new file hello.js and copy in the following code:

console.log("Hello, World!");

This uses Node’s built-in console module to display a message in a terminal window. To run the example, type the following command:

node hello.js

If Node.js is configured properly, “Hello, World!” will be displayed.

End of PreviewSign Up to unlock the rest of this title.

Community Questions