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}`);
})
HTML files will be so long and it's gonna be so messy
Template engine Advantage
ejs: Embedded JavaScript Templates
npm install ejs
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});
})