Skip to content

Commit

Permalink
Learned about templating engines
Browse files Browse the repository at this point in the history
  • Loading branch information
hk-skit committed Dec 27, 2017
1 parent a780c5d commit eea020c
Show file tree
Hide file tree
Showing 5 changed files with 462 additions and 2 deletions.
5 changes: 5 additions & 0 deletions my_express/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
html
head
title= title
body
h1= message
21 changes: 21 additions & 0 deletions my_express/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ app.get('/', (req, res, next) => {
res.end(responseText);
});

const middlewareFactory = (name, end, skip) => (req, res, next) => {
console.log(`Middleware name: ${name}`);
if (end) {
// Middleware can invoke end anytime.
return res.end(`Ended by ${name}`);
}
if (skip) {
// Middleware can also skip entire stack and move to next route/stack.
next('route');
} else {
// next middle from the stack.
next();
}
};
const middlewares = ['A', 'B', 'C', 'D'].map((name, index) => middlewareFactory(name, false, index === 1));
app.get('/middleware', ...middlewares);

app.get('/middleware', (req, res) => {
res.end(req.originalUrl);
});

app.listen(8000, () => {
console.log(`Listening at port:`, 8000);
});
14 changes: 14 additions & 0 deletions my_express/template_engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const express = require('express');
const path = require('path');
const app = express();
const PORT = 8000;

const viewPath = path.normalize(__dirname, '/index.pug');

app.set('view engine', 'pug');

app.get('/', (req, res) => {
res.render(viewPath, { title: 'Hello Pug!', message: 'Pug is awesome.' });
});

app.listen(PORT, () => console.log(`Listening at ${PORT}`));
Loading

0 comments on commit eea020c

Please sign in to comment.