Sooner or later every developer wants to share their projects on the web with the rest of the world! This is where Node comes in.
If you’ve been learning JavaScript for frontend development, and feel ready to level up, you’ve come to the right place.
Before we dig in, make sure you’ve already spent substantial time learning JavaScript. You want to feel solid on functions, callbacks, and higher-order functions. Otherwise, your time here will be a deep struggle as you’re trying to learn competing concepts at the same time.
If you feel ready to get your Node on, dig on in.
What Is A Server
By the end of this post you’ll feel like you have superpowers! Writing server code usually seems like the hardest or strangest thing to beginners. We tend to imagine that a server is some mythical computer in a center somewhere deep underground. This couldn’t be further from the truth.
A server is just a file listening to requests from the outside world. That outside world can even be on your own computer. Part of what makes JavaScript a great first language is Node.js. Node is the same engine that makes JavaScript run in the Chrome browser running outside of the browser. This lets you use JavaScript code on any file system.
To run a node file, on a system that has Node installed, you type node FILE_NAME.js
and it executes the file. It’s like running a file inside of a <script> tag in the browser, except that you don’t need the browser!
Why is this so powerful? JavaScript inside the browser isn’t written to give access to your computer’s file system. Imagine if any website could access all the files in your Documents or Downloads directory…
Node, on the other hand, has modules written to give access to the internal files of the system. This allows your server to access HTML or JavaScript files stored on the server to send them down to a client. It’s pretty powerful.
One of the main challenges in learning Node is that you’ll be dealing with a lot of asynchronous code.
If you feel rusty on about callbacks and promises go back and practice.
The essence of node-style callbacks is different from other places you’ve seen them. They look like this:
getDataFromDatabase(dataToGet, function(err, data) {
if (err) {
//run error handler
} else {
//request was successful, do something with data
}
})
They start with the error, so they are called error-first callbacks as well. You pass in the callback function as a parameter to the initial database call (or any asynchronous call).
Then, if there’s an error, it hits the error block first and you handle the error. Otherwise, it goes into the else block and you can run whatever code you’d like because you know you have access to the data at that point in time.
Make sense?
What is NPM?
Another big challenge is learning to understand NPM or Node Package Manager. It gives access to a huge number of packages written by other developers around this world. This is a blessing and a curse!
It allows you to extend your projects easily. Yet, it means you’re adding a lot of code without understanding what it actually does.
When you’re still starting out I encourage you to look at the original source code of these packages when you begin using a new one.
Over time you’ll begin to see the same packages used a lot. Your understanding of how they are written will improve. This won’t happen over night, but it’s worth the exposure, so start early.
One piece of caution: I highly recommend that you use Node Version Manager or NVM for short, to install Node. It takes a tiny bit more work to set up, but will make your life so much easier in the long run.
The main file you’ll become quite familiar with is the package.json file, stored in the root directory of your project.
Inside this file are three main areas that you’ll want to become familiar with on any new project you explore.
The areas of package.json are:
- Scripts section – this is where you’ll store all of the command line scripts to run your server, run tests, or anything else you want your application to do.
- The dependencies section – this is where you’ll store the packages and versions that you want included with your application.
- The dev dependencies section – this is where you’ll store the tools that you as a developer need, but that don’t need to be included with the application.
You’ll get more familiar with these three areas over time, but get in the habit now of always looking at them before you begin to code.
Resources For Learn Node and Express
- I encourage you to spend at least a few days using Node without Express. It is a little frustrating, but it will show you where Node ends and Express begins. I’ve noticed during job interviews that a lot of newer developers don’t understand where this line is. This tutorial by Traversy Media is a great starting point.
- As usual, NetNinja has a fantastic series on Node and Express. I’d spend time looking through the Express docs for a few tutorials on routing and middleware. The concept of middleware can be a bit challenging. It’s powerful though: in a nutshell, you can use middleware to intercept requests and transform them if they fit a specific pattern. BodyParser is a great place to begin learning about middleware.
- There are few topics you should understand during this section, but they don’t require entire courses to master. The first is writing RESTful routes from this article on Codecademy. The second, is how Semantic Versioning works and how NPM uses semantic versioning.
- If you’ve purchased a Frontend Masters subscription (and I highly encourage it), then there are two courses I recommend. The first is Introduction to Node, and the second is How To Write an API using Node.
- I want to make sure you feel confident using the file system too! One of the major benefits of Node comes from this superpower! Devslopes.io has a Complete Node.js course. And Stephen Grider has a deep course called Advanced Node (this covers things like Redis and some AWS tools as well).
- The last few topics to cover are Encryption, Hashing, and Authentication. You don’t need to go deep on any of these topics right now, but how they work are common interview questions. Here are a few videos I would watch on the subjects:
- Public Key Encryption
- Web Encryption
- SSL
- Hashing
- Passport.js from Traversy Media
- OAuth from Net Ninja
As usual, my recommendation is to build lots and lots of small projects. Practice building a small server, creating routes, saving things to a local file, and sending them back to a client.
Get comfortable with async code! This is a challenge, and it’s something that beginners don’t practice enough. There’s a reason the most common take-home project is to build a RESTful API. It shows that you have an array of skills and can put the pieces of a server together.
As always, please leave any comments or questions below. Or you can reach out to me directly. If you enjoyed this post sign up for my newsletter!
Leave a Reply