Nodejs for newbies (Part-2)

BONJOUR CODERS,

A quick recap of the last tutorial:

Nodejs fundamentals
Expressjs Basics
Explaining app.js and package.json
What does “npm install” command do?
Running your First Node App

And now…

This time we will get quick lessons about how to…

Define REST calls
Send data (String, JSON, etc)
Render Views (html)

So, to get you started,

We use express frameworks router to define our REST calls.

How?

Let’s just try adding a small piece of code and find out what happens 🙂

Modify your app.js from the previous tutorial as following:

Now go ahead, fire up your application with the command “node app” and see the magic.

Open up your browser and type the URL: http://localhost:8080/

You’ll see:

a GET call from browser

Hurrayyyy!!!

That was neat and easier than expected, right? Well, that’s Nodejs for you.

But we’ve got a problem. We cannot just write all our GET, POST, etc calls in “app.js” itself. Not a good practice.

So, let’s go ahead and modularize our code and make some more cool calls to our Node app.

In the root directory, make a folder with name “routes”.

Add a file called “baseCalls.js” with the following code.

…and our simplified “app.js” will look like the following:

Explaining app.js:

Line 3:
Defining and initializing the router object. This will help us make REST calls. Focus on how we have defined this object with “exports” keyword. This is similar to making a variable publicly visible, meaning other can use this variable. HOW? We’ll come to that later.
Line 8:
We are stating that all the calls should end in “/” in the url hitting our node. So,
in our case, “localhost” + “:8080” + “/” becomes our base URL. You may define your custom base URLs suitable for your application.
Line 10:
Your intuition is spot-on! This line is actually importing our code from the path “./routes/baseCalls”.

Explaining baseCalls.js:

Line 1:
Remember we exported our router object in app.js (line number 3)?
Well, guess what? We need it here. So, now our app variable here has access to all the data exported in app.js.
Hence we can use app.router to use functionalities of the router object.
Line 3–24:
This is self stating code. We are calling route method of the router object.
This method takes one argument, i.e. URL path. With this, we tell our server that “If any call comes, with GET and POST method to the URL: http://localhost:8080/ please process this particular piece of code.”
Next, following to route method, we have another chained method called “all”. Here we can do the things that are supposed to be done no matter what type of call it is (GET, POST, PUT, etc). This method is optional.
Next, you’ll see GET and POST calls defined. These methods take a function with two parameters: “req” and “res” (names can be different) as argument.
Usage is defined in the function and is pretty much self explanatory.

Now, let’s fire up our server again and see what comes now! 🙂

Congratulations! You are getting a good hold of Node now.

What if we don’t want to send text data and instead render an HTML page?

Node has got you covered. 😀

Create one HTML file in the root directory. Keep it basic for now.

I’ve put the following content in my HTML file:

Modify your app.js as the following:

Here…

Line 10–11: We are telling that our view engine is html, and look for views in my root directory.

And now, you run the server again, refresh browser and KABOOOM!

Some links for your reference,

So, this is the end of this tutorial. Hope you had fun developing and learning some cool stuff.

Thanks a lot my fellow readers,

See you sometime soon, with new stuff to learn and share,

This is Yash Soni signing off. Have a great time ahead! 🙂

P.S.: The link to GitHub repo:

The link to GitHub repo: https://github.com/yashsoniweb/MyFirstNodeApp

Star it. Fork it. Clone it. Get into it!

Happy coding! :)
Show your support

Clapping shows how much you appreciated Yash Soni’s story.