reading-notes

EJS

Javascript library, inject variables in your routes

npm init -y npm install --save express body-parser cors ejs

var express = require(express);
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();
var path = require('path');

app.use(bodyParser());
app.use(corse());

app.set('views', path.join(_dirname, 'views')));
app.set('view engine', 'js');


app.get('/', function(req, res){
    res.render('index', {
        //foo: 'bar'
        people: [
            {name: dave},
            {name: jerry}
        ]
    });
});



app.listen(8000, function(){
    console.log('listening');
});

index.ejs

<h1>Hello <%= food %></h1> //equal indicates your setting a varible
<img src="<%= foo %>">
<a href="/<%= food.id %>/destroy"></a>

<ul>
<% for (var person of people) { %>
    <li><%= person.name %></li>
    <%> } %>
</ul>

<ul>
<% for (var person of people) { %>
    <% if(person.name === 'dave') { %>
        <li>this is definitely <%= person.name %>!!!</li>
        <% } else { %>
        <li>this is definitely NOT dave, this is <%= person.name %>!!!</li>
        <%> } %
    <%> } %>
</ul>