EXPRESS JS

Express JS guide

Express JS fundamentals

Install Express and Handlebars
1. Install Express => Npm install express
 
2. Install Handlebars for creating a Templating engine => npm install hbs
 
Server.js
const express = require('express');
const hbs = require('hbs');
const fs = require('fs');

var app = express();

hbs.registerPartials(__dirname + '/views/partials')
app.set('view engine', 'hbs');

app.use((req, res, next) => {
  var now = new Date().toString();
  var log = `${now}: ${req.method} ${req.url}`;

  console.log(log);
  fs.appendFile('server.log', log + '\n');
  next();
});

// app.use((req, res, next) => {
//   res.render('maintenance.hbs');
// });

app.use(express.static(__dirname + '/public'));

hbs.registerHelper('getCurrentYear', () => {
  return new Date().getFullYear();
});

hbs.registerHelper('screamIt', (text) => {
  return text.toUpperCase();
});

app.get('/', (req, res) => {
  res.render('home.hbs', {
    pageTitle: 'Home Page',
    welcomeMessage: 'Welcome to my website'
  });
});

app.get('/about', (req, res) => {
  res.render('about.hbs', {
    pageTitle: 'About Page'
  });
});

// /bad - send back json with errorMessage
app.get('/bad', (req, res) => {
  res.send({
    errorMessage: 'Unable to handle request'
  });
});

app.listen(3000, () => {
  console.log('Server is up on port 3000');
});
Views/Home.hbs
DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Some Website</title>
  </head>
  <body>
    {{> header}}

    <p>{{screamIt welcomeMessage}}</p>

    {{> footer}}
  </body>
</html>
Views/About.hbs
DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Some Website</title>
  </head>
  <body>
    {{> header}}

    <p>Some text here</p>

    {{> footer}}
  </body>
</html>
Views/Header.hbs
DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Some Website</title>
  </head>
  <body>
    {{> header}}

    <p>Some text here</p>

    {{> footer}}
  </body>
</html>
Views/Partials/Footer.hbs
<footer>
  <p>Created By Andrew Mead - Copyright {{getCurrentYear}}</p>
</footer>
Views/Partials/Header.hbs
<header>
  <h1>{{pageTitle}}</h1>
  <p><a href="/">Home</a>

<p><a href="/about">About</a>

</header>

GIT, GitHub, SSH and app deployment

Install GIT, a great tool for creating restore points.
1. Install Git => Git is a tool that will help you keep track of the changes on your site, great for retrieving previous coding stages. You can download it here: www.git-scm.com
Will be also essential to install Git Bash, for the SSH key creation.

2. Run Git => Git init

3. Check Git Status => Git status

4. Add file or folders to check to Git Status=> git add package.json <= Or whatever file or folder you wanna add

5. Create a GitIgnore file => This is optional but good, you can create it in the main folder of your project and add it into Git status

6. Create your first Commit, by typing: git commit -m 'Here is your message' => This is a way to create a restore point for your project.
Deploy your app on GitHub (SSH key).
In order to deploy your app on GitHub you need to open a GitHub account and creating an SSH key through the terminal.

You can find all the procedure in details, by googling this "GitHub SSH".

Heroku app deployment

Deploy your app on Heroku
1. Create an account on Heroku => www.heroku.com

2. Download and install the heroku toolbelt => toolbelt.heroku.com

3. Open the terminal and run this command => Heroku --help

4. Then run the login => heroku login <= The you can insert the credentials of your heroku account

Test your App with MOCHA

Mocha installation and testing steps.
1. Install Mocha=> https://mochajs.org/