Table of contents
Installing NodeJS
Installing Manually
Before starting you need to have NodeJS installed, if not then you can install the latest version of Node over at nodejs.org/en/download/
Installing using the Terminal
If you feel that it's easier to install Node from the terminal, you can use the commands:
$ sudo apt install nodejs
$ sudo apt install npm
This installs NodeJS and NPM (Node Package Manager)
To check if they have been installed successfully, run the commands:
$ node -v
$ npm -v
This will show their versions and it should run without any error.
If you do run into a problem, I recommend switching your way of installing, Installing directly from the site is comfortable for new developers who have no experience in coding, while installing from the terminal is more comfortable for already experienced devs.
What's a NPM anyway?
Npm is the package manager for the Node JavaScript platform. It puts modules in place so that node can find them, and manages dependency conflicts intelligently.
Starting your Coding Journey
Cool, we have NodeJS and NPM installed, and all we need are some skills for making an express app.
You don't need to have any knowledge of Javascript for following the tutorial, but I do recommend you to learn the basics of Javascript first.
What's a Express anyway?
Express is a back-end web application framework for Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs.
Setting up the editor
Open up your favorite code editor, and make a new folder called Express-App, and inside the Express-App folder make a file called index.js
Installing express
Open up your terminal and run
$ cd Express-App
$ npm install express
This should navigate to the Express-App folder and start installing express
Start coding
Open up the index.js file in your favorite code editor and import the module by typing in
const express = require('express')
This will make a new constant variable called express, and you can use the basic express functions from it.
Now, we want express to build a webpage for us, to do that we need to call the express() function in a variable called app, just for convenience.
let app = express()
Where will our express app run on though?
By default, the express app runs on localhost:port or 127.0.0.1:port where the port is assigned in the code itself
What's a port anyway?
A port number is a unique identifier used with an IP address. A port is a 16-bit unsigned integer, and the total number of ports available in the TCP/IP model is 65,535 ports. Therefore, the range of port numbers is 0 to 65535.
To run our express app in a specific port, say 8080, we will need to add this to the end of the file
app.listen(8080)
And, to make sure our app is running, lets add a callback, so change the last line to
app.listen(8080, () => {
console.log('app running on port 8080')
})
Running a Node project
To run a Node project, open up the terminal (while still being in the Express-App directory) and enter the command
$ node index.js
This will run the program and you should see the output as
app running on port 8080
But we haven't coded an endpoint yet, so that's what we will do now.
Coding REST endpoints
There are many types of REST requests, but in this tutorial we are only going to be using the GET
Open the index.js file and add the following lines right above the app.listen()
function:
app.get('/', (req, res) => {
res.send('Hello World!')
})
This will display the text Hello World! On the web page
Re-run the Node file (By pressing ctrl+c) and typing in
$ node index.js
After you see the output as
app running on port 8080
Open your browser and go to localhost:8080 and you should be seeing this:
Passing Parameters in a URL
Yea, that's cool, but not cool enough to show your friends, not yet.
To make our project cooler, we will be passing in some input directly from the URL. For example, let's say we want to add an exclamation mark at the end of a word when we put the word at the end of a URL, say localhost:8080/exclamation/word which should display "word!". So let's start coding it.
To pass in special parameters like "word" in the above URL which can be changed into any word, we can't just start making a thousand GET requests for every word, instead, we will get the word by putting a parameter called ":word" at the end of the GET request in the code.
Add this right below the app.get()
function:
app.get('/exclamation/:word', (req, res) => {
res.send(req.params.word + '!')
})
As you can see, we are defining a dynamic variable called word (starting with ":", this is how express knows that the part of the string we are defining is a dynamic variable). And to retrieve the variable we are using the req.params.word, here req.params is an object of various dynamic variables in the URL.
Re-run the program and go to localhost:8080/exclamation/Hi and you should see:
Now that is cool. Additionally, you can add %20 to make a space and add more words, like this:
If you are new to NodeJS, there are many awesome blogs, tutorials, and people out there to help you accomplish your goal, whether its a hobby or a project.
The whole project can be found at github.com/NeevJewalkar/Express-App
Happy coding!