View on GitHub

reading-notes

CodeFellows Class Reading Notes

Intro to EJS in ExpressJS

Getting Started

In the Terminal:

In Server.JS -var express = require(‘express’); -var bodyParser = require(‘body-parser’); -var cors = require(‘cors’); -var path = require(‘path’);

-var app = express();

-app.use(bodyParser()); -app.use(cors());

-app.set(‘views’, path.join(__dirname, ‘views’)); -app.set(‘view engine’, ‘ejs’);

-app.get(‘/’, function(request, response) { response.render(‘index’); })

-app.listen(8000, function() { console.log(‘heard on 8000); })

Path is a core module of Node which joins two paths

Then create a folder called ‘views’ containing a file called index.ejs

In Index.EJS

-Add test text such as <h1>Hello World</h1>

Last but not least, turn on the server!


Evaluate an Injected Variable

response.render takes 3 parameters:

In Server.JS

In Index.EJS

Then turn on the server

<=% ejs %> works kind of like

Iterate/Loop Over an Array of Values

In Server.JS

In Index.EJS

Turn on the browser and see both names render on the page in an UL


If/Else Statements

In Index.EJS


Home