Get in Touch with Express.js

Ikhda Muhammad Wildani
Basho Code
Published in
4 min readApr 23, 2019

Hi, we will learn about Express.js right now and before we going any further, we must install Node.js in our computer. Go to the website and download it!

I use ubuntu 18.04

After we install the Node.js, we will automatically have NPM, and let’s started to make express app.

If you wanna check your version of node and npm just type this in the terminal

node -vnpm -v

Oh, I forgot about our canvas, we must have code editor and I using visual studio code which is have intregrated terminal and this will help us a lot.

So, open the code editor, and open the terminal with ctrl+shift+` (control shift backtick)

backtick is placed before number 1 in your keyboard

Now, let’s type npm init in the terminal, this command will help us to make node js app

Then, you will got question from the terminal. You can just ignore with press enter or you can type some kind of words, there is the name of your project or the description and so on.

But, if we make some real app, don’t forget about author field.

After that, you will get this new file called package.json,

So, then we need to install express to our project by typing this command

npm install --save express

We got one file called package-log.json and folder node_modules, our preparation has done.

Now, we will create a new file and give it name app.js give the code looks like

First, we import express module and store it in constant because we will not re-assign this variable in the future.

Second, we use the express module, because express is a function so we need to call this function and store it to the constant called app (or anything you want, but app is most commonly used)

Then, we can create some route to our web app, with using

app.get(firstArgs, secondArgs);

First Arguments is the route (address of our web) we want, and the Second Arguments is function that have two parameters (requests and responses).

res.send("some kind of message")

Then, in the second arguments we can pass the face of our web, but right now I just wanna said “hello world” for the home page and “I am Express.js” for about.

And the last thing is what is port ? Port is environment that our node.js listening on

process.env.PORT || 3000

This mean, that’s we don’t have any environment, then the default is port 3000, we can passing any number in here instead of 3000, we can use 4000, 8080, and so on.

And then we listen on that port, with passing the constant of port and the function, inside the function we just calling console.log and pass the port into the console.log.

Then, to checking our web app is work or not, we call it by typing

node app.js

So, go to the browser and try to reach the web by write

localhost:3000

And this will we got

Let’s try to another page

localhost:3000/about

Finally, we made it. So, how about Express.js ? Do you love it ?

--

--

Ikhda Muhammad Wildani
Basho Code

I like Lee Ji Eun (IU) and I wish someday I can sing together with her.