Node.js & Express
Super Easy Web Apps
Javascript Everywhere Kitchener-Waterloo
October 8, 2013
Logan Fuller
@logan_fuller
Slide Deck: oik.io/nodeandexpress
What is Express?
- Powerful, extensible framework for web apps
- Similar to Sinatra / Rails
- Middleware based
- Great community support
- Lets you focus on your website instead of boilerplate
Is it Production Ready?
Getting Started
Install Express
npm install -g express
Initialize your application with whatever extras you'd like
express --sessions --css less --ejs myapp
Install your dependencies
cd myapp && npm install
All finished!
node myapp
Cool... but what just happened?
Basic Structure of an App
package.json
{
"name": "myapp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.0",
"ejs": "*",
"less-middleware": "*"
}
}
app.js
Everything is
Middleware
Routes - user.js
/*
* GET users listing.
*/
exports.list = function(req, res){
res.send("respond with a resource");
};
Routes - index.js
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
</body>
</html>
Setting Up Your
Express App
App-Level Configuration
app.set('title', 'Javascript Everywhere');
app.get('title');
// => "Javascript Everywhere"
app.enable('trust proxy'); app.get('trust proxy'); // => true
app.disable('trust proxy');
app.get('trust proxy');
// => false
Environment-Specific Config.
// all environments
app.configure(function(){
app.set('title', 'Fancy Web App');
})
// development only
app.configure('development', function(){
app.set('db uri', 'localhost/dev');
})
// production only
app.configure('production', function(){
app.set('db uri', '255.255.255.255/prod');
})
View Engines
Initialize your view engine (in this case - EJS)
app.set('view engine', 'ejs');
Equivalent to:
app.engine('ejs', require('ejs').__express);
Writing Your Own
Middleware
Basic Middleware (CSRF Check)
// Check that a unique token is present in a request
function checkForCsrf(req,res,next) {
//use param, e.g., if token is valid proceed with next();
if(req.param("_csrf") === req.csrfToken) {
next();
} else {
next("Invalid CSRF token")
}
});
Error Handling Middleware
- Takes four arguments - err, req, res, next
- Should appear low in your middleware stack
function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
};
Using Middleware in Your App
app.use(function(req,res,next) { // checkForCsrf
//use param, e.g., if token is valid proceed with next();
if(req.param("_csrf") === req.csrfToken) {
next();
} else {
next("Invalid CSRF token")
}
});
Request Handling /
View Routing
- Remember - it's just middleware!
Basic Request Handling
Send JSON string for GET request:
app.get("/json", function(req, res) {
res.json({
x: "A variable!",
y: 1,
z: true
});
});
Send 404 when no document is found:
app.get("/documents/:id", function(req, res) {
// Nothing found!
res.send(404);
});
Thanks!
Questions?
Logan Fuller
@Logan_Fuller
Node.js & Express
By Logan Fuller
Node.js & Express
- 1,888