Create Project from Template
- NPM car on va installer globalment
npm install express-generator -gorsudo npm install express-generator -g
- yarn est just pour le projet
- Dans le dossier de vos projet, genere le structure backend
express --no-view --git ./
- commence une project a zero avec c'est command
yarn install
yarn installcors - inside app.js
yarn add corscors: est le gatekeeper pour le backend un securitie - ajouter cors a ligne 10
app.js
const cors = require("cors");
app.use(cors());
- install dotenv
yarn add dotenv
implementation: ligne 1 app.js (once and we can use it everywhere)
require('dotenv').config();apres ca c'est ne pas necessaire a importer apres
how to use, ex:
const connectionString = process.env.CONNECTION_STRING;
- 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}`);
});
- package.json replace script (dependent on step 6)
"scripts": {
"start": "node server.js"
},
- launch app
yarn start
- runs on : http://localhost:3000
Create random hex for SECRET_KEY
- go to node in the termainl by just typing
node
- if not installed
yarn add crypto
require('crypto').randomBytes(64).toString('hex')- copy string and paste to env var.
Install NVM and Node on MacOS
- install nvm (through brew)
brew install nvm- uninstall
brew uninstall nvm
- install node
nvm install node
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")));
- This also seems to work
app.use(express.static('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();
});
}