Express application with express-ejs-layout template
Contents
1 Environment
|
|
1 Install express.js
|
|
2 Simple application
|
|
Start server:
|
|
Then you can access http://localhost:3000/
to via browser.
Comparing to node.js simple web server, many step is hidden under the library.
3 Application using express-ejs-layouts
3.1 Install packages
ejs
and express-ejs-layouts
support express and ejs web page template.
|
|
3.2 Use ejs template
Code structure
|
|
For using ejs template, main.js
calls:
|
|
views/layout.ejs
defines the template, like:
|
|
Then the other pages like views/index.ejs
supplement the <%- body %>
parts at layout.ejs
like:
index.ejs
|
|
3.3 Run server
As I installed nodemon
by npm i nodemon -S
and add below to package.json
,
|
|
Start server by:
|
|
Then web page is generated by template.