Express Flashcards
What is a framework?
Embodies the concept of Inversion of Control (IoC).
Fill in white spaces.
Framework Calls You
Process to setup Express
> npm install express --save var express = require("express") var app = express();
start express
app.listen(PORT, IP, function)
define a get route in express
app.get(‘/route-path’, function (request, response) {});
Express
how to return something in get request
within call back function call response.send(content)
Express
How do you define a route that matches routes not specified
at the end place:
app.get(‘*’, function(req, res) {});
Express
How do you define url path variables
use colon + var_name
app.get(‘/r/:subreddit/comments/:id
Express
Using route params
app.get(‘/r/:subreddit/comments/:id, function(req, res){})
you access subreddit and id via req.params.subreddit and req.params.id respectively
What does EJS stand for?
Embedded Javascript
Express
How do you return a template in a route callback
res.render(“template/path”)
Is EJS natively supported by express?
No. you need to install with npm, but you don’t need to require it because you don’t call it directly.
Express
How do you pass and use a variable to EJS template in Express?
In express file
res.render(‘template.ejs’, {varName: var})
In template.ejs
What types of ejs tags are there?
render content to html
evaluate but don’t render
Express
How do you pass data in from a form to the server?
- set form action to the post route
- install npm package body-parser
var bodyparser = require(‘body-parser’)
app.use(bodyparser.urlencoded({extended:true}));
app.post(‘/route’, function(req, res) {
req.body
})
Express
How can you prevent from having to retype the .ejs
for each template?
app.set(“view engine”, “ejs”);