Tutorial for Hello World in Node Express step-by-step

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In this tutorial, we write a Hello World program in Node.js with ExpressJS.  It assumes that you have already installed Node and are able to run npm. If you’ve installed it long time ago and want to update to latest version as of this writing (which is 4.6.0), you can install it again.

Step 0:

Open up the terminal on the Mac.  For windows, run the  “Node.js command prompt” that was installed.  From this point, we will just call this the “terminal”.

Step 1:

Create an navigate to an empty project folder.   I have mine in c:\temp\hello.   And then type “npm init” and press enter through all the default prompts ending in typing in “yes” to confirm the creation of the package.json file.

npm init

Step 2:

The package.json contains information about the node application and its dependencies.  If you open up the newly created package.json, you see that the main entry point to your application is set to “index.js” Although you can change this to something else (app.js and server.js are common), we keep it since that was the default generated by “npm init”.

So create new file in “hello” folder named “index.js” with the following content …

require express

This pulls in the express module and create an instance of it into a variable “app”.  Express is an npm module that runs on top of Node.  It makes writing web apps much easier; and so commonly used that it is the “E” of the MEAN stack.

Step 3:

We need to install “express” using by typing this in command prompt…

npm install

The –save option tells it to also alter your package.json to mark express as a dependency of the app…

save flag

Step 4:

Now write the rest of the index.js as follows…

complete hello app

This is the complete code for the “Hello World” Node Express app.

Step 5:

The app.get tells express that if user goes to the root route of the application, then run the function handler.  This function handler has two parameters conventionally called “req” (for the request to the server) and “res” (for the response from the server).  Within this function, we write “res.send” to send a response from the server.  The response is the string “Hello World”.  By default, the return code is 200, indicating a good response.

Step 6:

The app.listen statement is what starts the web app listening on port 3000.  It consoles out a feedback statement to the person starting up the web app.

Step 7:

To start the web app, run node in the terminal with the index.js file …

run app

And you see the feedback of listening on port 3000 due to the console.log statement in the code.

Step 8:

That means that you can open up your browser to this URL and see the hello world output…

hello world output

Step 9:

To terminal the web app, do Ctrl-C on the terminal.

You are running the app locally.   A webapp is no good if it is not publicly available.  In the next tutorial, we will host this app on Heroku and publish it on the internet.