Last active
June 27, 2018 15:35
-
-
Save yukeehan/a28bf3271f110e5f66807b8fc5f55f7b to your computer and use it in GitHub Desktop.
EJS Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require("express"); | |
var app = express(); | |
app.get("/",function(req, res){ | |
res.render("home.ejs"); | |
}); | |
app.get("/love/:thing",function(req, res){ | |
var thing = req.params.thing; | |
res.render("love.ejs",{thingVar: thing}); | |
}) | |
app.get("/posts", function(req, res){ | |
var posts = [ | |
{ title:"post1", author:"yukee"}, | |
{ title:"post2", author:"yukee"}, | |
{ title:"post3", author:"yukee"} | |
]; | |
res.render("posts.ejs",{posts: posts}); | |
}) | |
app.listen(process.env.PORT, process.env.IP); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>This is the homepage</h1> | |
<!-- ejs files should be in views directory --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>I love <%= thingVar.toUpperCase() %></h1> | |
<% if(thingVar.toLowerCase() === "apple"){ %> | |
<p>Good Choice! Apple is the best!</p> | |
<% }else{ %> | |
<p>Bad Choice! You should pick Apple!</p> | |
<% } %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>This the post page</h1> | |
<% posts.forEach(function(post){ %> | |
<li> | |
<%= post.title %> - <strong><%= post.author %></strong> | |
</li> | |
<% }) %> | |
<!-- ejs files should be in views directory --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment