What is Node.js and Should I Learn it in 2021?
This is why Node.js came to save the day!
What is a Runtime?
How to Install Node.js
Go to the official Node.js website and download the LTS version which is recommended for most users.
Once that has installed you should be able to see what version of Node.js you are running by typing 'node -v' in the terminal.
// Returns 'v14.16.0' as of 17th May 2021
If you are getting a version number after typing that command, you are officially running Node.js. Congrats!🎉
Is Node.js Easy to Learn?
Let's build a Node.js app to show you what I mean.
Instead of using the browser, we can execute this code in the terminal.
What Can Node.js Be Used For?
Node.js is a great choice to use for building API's and server-side logic. Let's build a server that uses Node.js!
To start creating a Node.js server you must run 'npm init' in the terminal of your JS application.
NPM is Node Package Manager which is used to manage dependencies that your application relies on. It is what we use to install, update and delete packages that you use in your app. NPM comes installed with Node.js so you don't have to worry about installing it separately.
This is the sort of information you can modify in the 'npm init' process. I just left it all to the defaults which you can also do by typing:
npm init -y
After you have run this command you will want to install Express which is a Node.js framework that makes it quicker easier to get a server up and running.
This can be doing by typing:
npm install express
We can build a simple server that sends back a response when you visit "localhost:3000".
Let's break this code down!
Lines 1-2 is showing us requiring the Express package and assigning it to a variable called 'app' which we can use later.
Lines 4-6 is using the 'app' variable to run a function built in to Express which gets the index page and sends back a response containing a HTTP status code 200(OK) and some text "Hello world!".
The 'app.get' function has 2 parameters. One is the "/" which indicates the index page and the second is parenthesis with 'req' and 'res' in them. The 'req' is short for request which can get information from the incoming request from the client. The 'res' is short for response, which is what you can send back to the client when they visit index page.
Lines 8 - 10 is using the 'app.listen' function which takes in a parameter of 3000 meaning that we will listen to any incoming requests on port 3000. You can create a callback function like I have and send back a console log to say the 'app.listen' was successful.
Are you ready to see what all 10 lines of code creates on localhost:3000?
Impressive right? 😅
Don't worry there is a lot more that goes on under the hood!
Node.js comes with lots of cool modules to help you bring out your server-side app to life. Why not have a go at creating a Node.js API that will send back data from MongoDB or a MySQL database.
Here's some good resources I'd recommend when you are wanting to learn more about Node.js and API's.
- Fireship - RESTful API's in 100 Seconds YouTube video.
- Dev Ed - Build a RESTful Api with Node.js Youtube video.