Create Project from Template

  1. NPM car on va installer globalment
    • npm install express-generator -g or
    • sudo npm install express-generator -g
  1. Dans le dossier de vos projet, genere le structure backend express --no-view --git ./
  1. yarn install yarn install

  2. cors - inside app.js yarn add cors cors: est le gatekeeper pour le backend un securitie - ajouter cors a ligne 10

app.js

const cors = require("cors");
app.use(cors());
  1. install dotenv

yarn add dotenv

  1. create server.js entry point
    • place this file in the root of the project
    • server.js
const app = require("./app"); // Import the configured app
const PORT = process.env.PORT || 3000;

// Start the server
app.listen(PORT, "0.0.0.0", () => {
  console.log(`Server running on http://0.0.0.0:${PORT}`);
});
  1. package.json replace script (dependent on step 6)
"scripts": {
    "start": "node server.js"
},
  1. launch app

yarn start

Create random hex for SECRET_KEY

  1. go to node in the termainl by just typing node
  1. require('crypto').randomBytes(64).toString('hex')
  2. copy string and paste to env var.

Install NVM and Node on MacOS

Step 1 setup npm

From terminal and inside project directory npm init -y

Step 2 install Express

From terminal and inside project directory npm i express

Step 3: Server nodemon

From terminal and inside project directory npm i --save-dev nodemon

in scripts of package.json "devStart":"nodemon server.js"

To run app (from terminal and inside project directory): npm run devStart

create a server.js file with server app

Step 4:

Simple server.js:

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

app.get("/", (req, res) => {
  res.send("We up!");
});

app.listen(3000);

Step 5: import ejs

This is library used to render HTML files install in environment (from terminal and inside project directory): npm i ejs

In server.js: app.set('view engine', 'ejs')

create a /views directory and make all the files using .ejs extension instead of .html

.
├── node_modules
├── package.json
├── package-lock.json
├── server.js
└── views
    └── index.ejs

Optionals

Add CSS to project

create styles.css in ./public/

in your head section: <link rel="stylesheet" href="style.css">

in server.js:

var path = require("path");
app.use(express.static(path.join(__dirname, "public")));

Middleware functions

Example if you have a route that is a post and get and it has a table that uses array data, instead of writing the function in each route, you can pass the funciton in middleware and pass the resulting variables before each route.

app
  .route("/")
  .get(customFunction, (req, res) => {
    console.log("- in GET / route -");
    customArray = res.locals.customArray;
    console.log(Object.keys(customArray));
    res.render("index", { customArray: customArray });
  })
  .post(customFunction, (req, res) => {
    console.log("- in POST / route -");
    console.log(`Object.keys(req.body): ${Object.keys(req.body)}`);
    customArray = res.locals.customArray;
    res.render("index", { customArray: customArray });
  });

function customFunction(req, res, next) {
  customFunctionThatReturnsAnArray().then((customArray) => {
    res.locals.customArray = customArray;
    next();
  });
}