Skip to content

Instantly share code, notes, and snippets.

@yukeehan
Last active June 27, 2018 15:35
Show Gist options
  • Save yukeehan/a28bf3271f110e5f66807b8fc5f55f7b to your computer and use it in GitHub Desktop.
Save yukeehan/a28bf3271f110e5f66807b8fc5f55f7b to your computer and use it in GitHub Desktop.
EJS Demo
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);
<h1>This is the homepage</h1>
<!-- ejs files should be in views directory -->
<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>
<% } %>
<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