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

app.use(express.urlencoded({extended: false}));
app.use(express.json())

app.use(express.static('public'));  // to offer static files

app.use((req, res, next) => {  // middleware before going to router.
    // console.log(req);  // see all request
    next();  // finish middleware and go to next step.
});

// before using template engine
app.get('/', (req, res) => {
    res.send('<!DOCTYPE html>\\
    <html lang="en">\\
    <head>\\
        <meta charset="UTF-8">\\
        <meta http-equiv="X-UA-Compatible" content="IE=edge">\\
        <meta name="viewport" content="width=device-width, initial-scale=1.0">\\
        <title>Document</title>\\
    </head>\\
    <body>\\
        Hi. I am with html<br>\\
        <a href="/hi">Say Hi!</a>\\
    </body>\\
    </html>')
});

app.listen(port, () => {
    console.log(`Listening at <http://localhost>:${port}`);
})

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f00cd359-79bf-43d2-bc39-8cb75b5a7ce5/Screen_Shot_2021-06-22_at_12.54.04_PM.png

HTML files will be so long and it's gonna be so messy

Template engine Advantage

To use Template engine

ejs: Embedded JavaScript Templates

npm install ejs

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4f3b61bb-c2bd-41ca-a6ed-da831dcb4444/Screen_Shot_2021-06-22_at_12.58.21_PM.png

index.js

// using ejs
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/test', (req, res) => {
    let name = req.query.name;  // ?name=garden

    res.render('test', {name});
})

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1552c057-803d-44d3-a54b-6c6e37a1cfb6/Screen_Shot_2021-06-22_at_1.06.41_PM.png