Introduction to Node.js – Hello world with ExpressJS


Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /var/www/blog.mzaferyahsi.com/html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Welcome to the first article of Introduction to Node.js series.

I wanted to write my first blog about writing a simple Node.js based API with ExpressJS framework. I am assuming that you know and have already installed Node.js! If you don’t know, you better Google before we start 🙂

ExpressJS is a “Fast, unopinionated, minimalist web framework for Node.js“. It has many features but today, we’ll be using it to expose a simple “Hello world!” api. I’ll be using my Mac but I’ll also provide corresponding commands for Windows. For development IDE, as a long time C# developer, I’ll be using Microsoft’s new multi platform IDE Visual Studio Code. Feel free to download it. But you can use any IDE, even a simple text editor, to complete this course.

To get started, open the terminal and navigate to the folder that you’d like to work. Then create a folder named “hello-world-nodejs” and enter into it.

After creating the folder, let’s initialise our npm file by typing the command on the terminal.

You’ll be seeing a screen like below.

npm_init

npm init

The text in parentheses is the default value that npm provides. Feel free to fill the information however you like. The only important value will be the “entry point” which should be set as “server,js”. It is being used as an approach in Node.js to have the main application code in server.js file. In the end, you should see a screen like below.

nom_init_complete

npm init

This operation creates a JSON file which contains all the necessary information about our package. The content of the package.json file should be like below.

Before continuing, it’d be better for me to explain some additional information that will make you understand the structure of Node.js a little bit better. Node.js works by modular structure, every external package or some codes that we will write can and will be treated as module. Now you are wondering what module means, well Cho S. Kim has made a good explanation about module on Node.js in this article.

A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file.

If you are coming from Object Oriented Programming, this would make sense to you. By using modules, your application will be separated into small (preferred, but can be big) parts.

Now we can start adding our dependencies to our package, which is only ExpressJS at the moment. Just enter the following command to add ExpressJS as dependency to our package and save it to “package.json” file.

The “–save” command tells npm to save this package as dependency to our package.json file. If you check the content of the package.json file, you’ll notice the new block below.

And you can notice the “node_modules” in our directory. It contains depend packages and other packages that are needed by Node.js. As mentioned earlier, external modules are stored in “node_modules” folder.

hello_world_folder

Folder contents

Now we can open our IDE and start writing some code! The window that you’ll be seeing should be like below. But if you can’t see the “Explore” window don’t panic. Just open it by clicking the page icon on the left.

vs_code_init

Visual Studio Code

Then click the “Open Folder” button and browse to our project folder and click “Open”. Your “Explore” window should look like below. Create server.js file by clicking the “New File” button.

explore window

Explore window

Now, we need to initialise our ExpressJS. In order to do this (and also load other modules), we’ll be using “require” keyword. “require” takes only one parameter, which is the name or the location of the module. If you’ll use modules from “node_modules” folder, you’ll only give the name of the module. To initialise ExpressJS into a variable, we’ll be using the code below.

Now we need to create our application instance from ExpressJS framework.

After creating our application instance, we’ll add our “Hello world!” api with the following code.

ExpressJS gives us the extensibility to define http methods as a method in our application instance. By this code, we are creating a GET method for ‘/api/hello’ address which is the first parameter. The second parameter which is a function is called callback function. A callback function can be defined as a function that will be called when the required operation is executed. In our example, our callback function will be called when someone sends a GET request to ‘/api/hello’ endpoint of our api. I’m guessing that you can understand what request and response variables corresponds to.

Now that we know everything we need until this point, let’s finish writing our api. We’ll be returning a JSON object which has a message parameter with the value of “Hello world!”. Response object provides us some methods on returning data. I’ll only explain the “send” and the “json” methods.

With “send” method, you can return any data. Send is mostly used to return messages. ButwWith “json” method, you can return any JSON object.

Since that we’ll be returning JSON object, we’ll use “json” method. Both “send” and “json” methods take only one parameter, the content that you’d like to return. Our code which returns our JSON object should look like below.

Now that we have our first api, the only thing missing is to start our application. To start an ExpressJS application instance, you should use “listen” method, which will take the port that we’d like to run our application as parameter.

Now that we have our api ready, let’s start our application. Just type the code below to terminal/command prompt which we have opened before.

You’ll notice that nothing comes up on console but our app is running.

hello_world_running

Hello World app running

Let’s make a small add to the listen method to inform that the app has started.

You should see a screen like below on terminal/command prompt.

hello_world_log

Hello world app running with log

Our final “server.js” file content should be like below.

Now if you browse http://localhost:3000/api/hello from your browser, you should see our JSON object.

API Result

API Result

Congratulations!

You have successfully created your first basic API on Node.JS with ExpressJS framework.

To be continued..

P.s.: I didn’t upload the source code(s) to GitHub since you can find the whole code in this page.

Comments

comments