We're live-coding on Twitch! Join us!
How to use Nodejs - For Absolute Beginners (Part 1)

How to use Nodejs - For Absolute Beginners (Part 1)

In this series, we are going to be learning how to use nodejs to build a simple web application. We will learn the following;

  • How to create a server in node.
  • How to serve up a webpage using node.
  • How to use npm to manage our packages.
  • How to use express.
  • How to use ejs for HTML templates (super cool stuff)
  • How to handle requests for data from our server (GET, POST)

I’d be using sublime text for writing my codes. You can use anything you like. Just make sure you can easily use your terminal any time you want.

What is nodejs?

Nodejs is a super cool platform that allows us run javascript on a computer/server. Normally, we only run Javascript through web browsers, but nodejs has changed all of that. Now, you write javascript and run it everywhere.

**Technical Stuff:** Nodejs is built with C++, and Google’s V8 engine that allows Javascript code run on computers/servers. You can checkout more about Google’s V8 engine [here](https://developers.google.com/v8).

Nodejs uses Javascript, is super fast, has a huge ecosystem of open source framework and is amazingly easy to write. With web sockets, you can even go ahead and make chat apps with all the real time features. Sounds like the superman of platforms? Just keep it low so others don't hear you.

Here is what you need to serve a webpage.

var http = require('http'); 
   http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Hello World!');

No need for nginx, apache, php or all of those plenty things we would have got if we wanted to use any of the other platforms.

Alright, quickly follow this link and install nodejs. Try out your node installation by typing node -v in your command line/terminal. It should show you your node version.

Getting Started with our App

Let’s create our first node app and get things going. Shall we?

  1. Quickly go to your text editor and create a file app.js.
  2. Copy the code above into the app.js file.
  3. Change your working directory from your command line/terminal to where you saved the app.js file.
  4. Run node app on your command line/terminal.
  5. Open your browser and visit localhost:3000 . PS: Make sure nothing is connected on port 3000 so that your app can work. Change the port to suit your needs. If you have everything working fine, congratulations! You just made your first node app

Next up Making a Server and serving data to browser

Like this article? Follow @chiefoleka on Twitter