In this Node.js tutorial series we are going to learn how to write our first server-side Node.js app using the Express framework so we can serve up static and dynamic web pages using JavaScript.
Node.js allows us to use JavaScript to write server-side applications. Express is a framework that makes handling routes, using middleware, and doing other server-side stuff much easier in kind of the same way that jQuery makes manipulating the DOM much easier than writing vanilla JavaScript on the front-end.
Installing Node.js
The first thing we need to do to start using building Node apps is to dowload and install the latest version of Node.js.
If you already have Node installed you can skip down to "Using Node in the Command Line". If you aren’t sure if you have Node installed, open a command prompt and run node -v
. If you get an error, you need to install Node.js.
If you don’t have Node.js installed on your computer, go to nodejs.org to download the installer. You should notice that the page has intelligently detected your operating system and will offer the right download links for you. I’m using a Mac, so here is what I see:
There are two green download buttons on the page. One is labeled “LTS” for “Long Term Support”. The other is labeled “Current” which means it’s the latest release but it might not be suitable for production applications that need long term stability. Go ahead and download the the “Current” (latest) version since we will be using it for learning purposes. When you click the download button, it should download an installer program. Open it up and follow all of the steps. It should be pretty straightforward.
Once you’ve installed Node, you should be able to use it in your favorite command line tool. Let’s make sure the node
command is available to us. Open up a command prompt window and type node -v
. Then, hit the ‘Enter’ key to execute the command. This command checks what version of Node you have installed and is a great way to make sure you have it installed properly. If all goes well you should see a response in the command line that says v9.2.0
or whatever version number you downloaded from the Node.js website.
If you get a response like node: command not found
then something went wrong during the installation so you’ll need to Google to find some troubleshooting tips or just ask me for help. :D
Using Node in the Command Line
Now that we’ve confirmed Node is installed correctly, we can use it right from the command line. Simply run node
in your the command line tool. If all goes well, your cursor should drop down to a new line, preceded by a right-facing caret (AKA greater than symbol, “>”).
Now you can start writing some JavaScript. Try executing some math expressions. Type 1 + 1
and hit ‘Enter’. You should get the response 2
. Try executing console.log(“Hello Node!”);
or other JavaScript expressions.
JavaScript on the front-end (Browser) vs. the back-end (Node)
Keep in mind that while you can run most of the JavaScript you are used to using in front-end web development in the Node environment, there are certain methods that will not be available. For example, there is no HTML in Node, so we can’t use any DOM methods, like document.getElementById();
. There is also no such thing as alert();
or prompt();
in Node because these are browser based functionality, and Node doesn’t run in the browser.
Node also makes some new server-based functionality available to us, like http
, assert
, and the file system (fs
). Don’t worry too much about what these are for right now. You’ll get into them later on in your Node journey. (If you are interested in learning more about all of the functionality available in Node, check out the full Node.js API documentation.)
Alright! You are off to a great start! Check out my next post about how to set up a Node.js project with NPM!