Blog

Hello NodeJS | Hello World

Before we move on to more boring stuff like setting up or project properly, let’s do something fun! Creating a Hello World Application in NodeJS doesn’t require very much. Since we have the nodejs-package already installed on our system, all we need is a file called server.js in our project-folder with the following line in it:

By paranerd | Sep 17, 2018

Hello NodeJS | Installation

Welcome to this introduction to NodeJS! In the following chapters I covered all the basics you need to get started in development for NodeJS. I tried to be as precise as possible, leaving out all the cluttering bells and whistles in an attempt to give you a clear view of only the important parts.

By | Sep 17, 2018

Hello NodeJS | Mongoose

Using basic Mongo in our project is definitely doable but not very efficient in terms of maintainability. As you can see there’s a lot of boilerplate code required for even basic tasks as checking for a user.

Mongoose is a wrapper for MongoDB that makes it a lot easier to handle database-queries

By paranerd | Sep 17, 2018

Hello NodeJS | Organizing Views

So far we only have to manage two views - no big deal. But imagine having dozens of different pages, which is not at all unusual. Having all those views in one single views-folder would be a huge mess and very difficult to manage and maintain. Therefore it’s usually better to use sub-folders to organize our views.

By paranerd | Sep 17, 2018

Hello NodeJS | Project Structure

Before we go any further, let’s have a quick look into the general structure of a NodeJS-Project. When you search the web for suggestions on that, you will be flooded with a miriad of opinions on what’s the best way to organize your code. To be honest, I don’t claim perfection for my approach. I simply took the best out of all that I could find on this topic and what I came up with works great for me. So in the end you’re getting just another opinion here^^. Feel free to go out there and find your own way - just make sure that you actually DO have any sort of organization in your project and don’t just throw everything in one folder.

By paranerd | Sep 17, 2018

Hello NodeJS | Sessions

Sessions are a simple way of persisting information between page requests. They make it easy to save a value in some part of your website and retrieve that value at a completely different place without having to manually send it there.

A session is nothing magical, just an array of key-value-pairs saved on the server accessed by the browser via a Session-ID which is stored in a cookie. The Session-ID enables us to have different sessions for different users of our website at the same time.

By paranerd | Sep 17, 2018

Hello NodeJS | Templates

So far our pages have been very static, meaning we just deliver HTML without any dynamic content whatsoever. What we need is an easy way for our server to send data to the client. This is where templates come in.

There are many template engines available. Some of the more popular ones are:

By paranerd | Sep 17, 2018

Hello NodeJS | The Package Manager

With the installation of the nodejs-package also came a tool called ’npm’ (Node Package Manager). We will be using this quite a bit when developing for NodeJS. For a start it helps us setting up our project properly.

Now we can let the npm-magic happen:

1
npm init

This will ask you for a couple of things. You should at least enter your project’s name, the current version and your name as the author. Setting the entry point to server.js helps anyone working with your project to have this information easily accessible without having to search through all your files. The rest of the fields can be left blank for now.

By paranerd | Sep 17, 2018

Hello NodeJS | User Input

Displaying a login-screen is one thing, but it doesn’t help us all that much if we can’t process the user’s input, right?! Let’s add this feature now!

First, we need to install another package that allows us to extract data from POST-requests:

1
npm install body-parser

We need to include the body-parser in our server.js so we can use it:

By paranerd | Sep 17, 2018

Hello NodeJS | Working With HTML

So far we only sent plain text to the browser. Already kind of exciting but we want to be able to serve really cool HTML, don’t we?! Let’s go do that right now!

In our views/ directory we add a file called index.html:

1
2
3
4
5
6
7
8
<!DOCTYPE html>
<head>
    <title>My first NodeJS-App</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>

Now we modify our server.js and replace the old routing with this one:

By paranerd | Sep 17, 2018