Lesson 1: Hello World!

1 Create the package.json

We need to install the node dependencies first. Create a new folder and add a package.json file in which you paste the following code.

Then open your console and type npm install. This will create a node_modules folder with all the packages we need.

{
  "name": "js-course",
  "version": "1.0.0",
  "description": "The Code at Uni JavaScript Course",
  "main": "app.js",
  "scripts": {},
  "author": "Code At Uni",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "express-handlebars": "^3.0.0",
    "mailgun-js": "^0.7.12"
  },
  "devDependencies": {
    "livereload": "^0.5.0"
  }
}
Copy to clipboardpackage.json

2 Importing the dependencies

We first need to import Express, so we can use its functions and capabilities. We assign it to the variable express.

After importing Express, we can call the function express() and assign it to a new variable called app which we will use as our main variable.

// Load external dependencies
var express = require('express');

// Ready up Express so we can start using it
var app = express();
Copy to clipboardapp.js

3 Creating the first route

Any website needs at least one route: the root path, which is when you only have the domain: mywebsite.com/

For now we are only gonna display the string 'Hello World!'.

// Homepage
app.get('/', function (req, res) {
  res.send('Hello World!');
});
Copy to clipboardapp.js

4 Starting the server

Now that we have everything set up, we just need to start the server.

Just open the command line, navigate to the folder that has the file app.js and run the command:

node app.js
// Start our server on port 5000
app.listen(5000, function () {
  console.log('Lesson 1 listening on port 5000!');
});
Copy to clipboardapp.js