Creating a Node.js Project

First create a project folder and navigate to it using terminal once there type:

npm init

It will ask for project name etc. and all of those information will be saved on package.json, you can change them later on if you like to, so for now just create a file and named it app.js and put this code in there.

let message = 'lorem ipsum';
console.log(message);

On your command line run node app.js then you will see the message from above and that is the most basic way to set up a node project.

Using Express with Node

In the following we are going to re-write the above just so it uses express so we can the acces the application on the browser

First we need to install express like so:

npm install express --save

Once you are done you will see that expressed has saved as dependency on package.json like this:

{
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.15.2"
  }
}

To use the library you call the require() function as shown below in your app.js file.

const express = require('express');
const app = express();

The first thing we need to do is to create a server where browser can connect to, we can do that with listen build in method that express provides, in this way:

app.listen(3000, function() {
  console.log('listening on 3000')
})

If you the run the application:

node run app.js

You can then navigate to localhost:3000 there you will see a message saying "cannot get /" this is because we need to do more implementation for the specic paths.

In Express we handle GET methods with get method like so:

app.get(path, callback)

The first argument, path, is the path of the GET request. It’s anything that comes after your domain name. When we’re visiting localhost:3000, our browsers are actually looking for localhost:3000/. The path argument in this case is /. The second argument is a callback function that tells the server what to do when the path is matched. It takes in two arguments, a request object and a response object:

app.get('/', function (request, response) {
  // do something here
});

Using ES6 this will be:

app.get('/', (request, response) => {
  // do something here
})

So the all code will be like the following:

const express = require('express');
const app = express();
let message = 'lorem ipsum';
app.get('/', (req, res) => {
  res.send(message)
});

app.listen(3000, function() {
  console.log('listening on 3000')
})

Serving an html page on GET

To server an html page on node get method we do this:

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
  // Note: __dirname is directory that contains the JavaScript source code. Try logging it and see what you get.
})

So far we had to do node app.js each time we made changes to app.js this way is very time cosuming. Nodemon restarts the server automatically whenever you save a file that the server uses. We can install Nodemon by using the following command:

npm install nodemon --save-dev

The reason we’re using a --save-dev flag here is because we’re only using Nodemon when we’re developing. This flag would save Nodemon as a devDependency in your package.json file. There is another way of using it to run nodedemon:

$ ./node_modules/.bin/nodemon app.js
$ ./node_modules/.bin/nodemon app.js

Another way is to create a script key on package.json

{
  "scripts": {
    "dev": "nodemon app.js"
  }
}

When we run npm run dev it will triger app.js.