Node.js with Express
web application framework for node
Gergely Nemeth
What is Express?
get '/' do
redirect to('/hello/World' )
end
get '/hello/:name' do
"Hello #{params[:name]}!"
end
Gergely N...
Getting started
Gergely Nemeth
● npm install express
var express = require('express');
var app = express();
app.get('/hell...
Getting started #2
Gergely Nemeth
● npm install -g express
● express --sessions --css stylus --ejs myapp
● create : myapp
...
Configuration
Gergely Nemeth
Used to separate production/development/etc...
configurations (based on NODE_ENV)
// all envi...
Middlewares
Gergely Nemeth
● logging, auth, gzip, csrf protection, session
management, etc...
● called sequentially, next(...
Routing
Gergely Nemeth
app.VERB(path, [callback...], callback)
● GET, POST, PUT, DELETE, PATCH
● [callback...] are middlew...
Sessions
Gergely Nemeth
● With middlewares
app.use(express.cookieParser()); app.
use(express.cookieParser('some
secret'));...
Rendering
Gergely Nemeth
● With template engines: Mustache(hogan), Jade,
EJS...
● Layout, views, partials
● Variables are ...
Rendering #2
Gergely Nemeth
● part.html
<h1>{{ title }}</h1>
● index.html
{{> part}}
Hello world!
Upcoming SlideShare
Loading in...5
×

Node.js with Express

1,476

Published on

Express: web application framework for node
Includes: getting started (install), configuration, middlewares, session management, routing and rendering

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,476
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Node.js with Express"

  1. 1. Node.js with Express web application framework for node Gergely Nemeth
  2. 2. What is Express? get '/' do redirect to('/hello/World' ) end get '/hello/:name' do "Hello #{params[:name]}!" end Gergely Nemeth ● Node.js web framework ● Sinatra-inspired
  3. 3. Getting started Gergely Nemeth ● npm install express var express = require('express'); var app = express(); app.get('/hello', function(req, res){ res.send('Hello World'); }); app.listen(3000); console.log('Listening on port 3000');
  4. 4. Getting started #2 Gergely Nemeth ● npm install -g express ● express --sessions --css stylus --ejs myapp ● create : myapp create : myapp/package.json create : myapp/app.js create : myapp/public create : myapp/public/javascripts create : myapp/public/images create : myapp/public/stylesheets create : myapp/public/stylesheets/style.styl create : myapp/routes create : myapp/routes/index.js create : myapp/views create : myapp/views/index.ejs
  5. 5. Configuration Gergely Nemeth Used to separate production/development/etc... configurations (based on NODE_ENV) // all environments app.configure(function(){ app.set('title', 'Rescue'); }) // development only app.configure('development', function(){ app.set('mongoUrl', 'localhost/dev'); }) // production only app.configure('production', function(){ app.set('mongoUrl', 'n.n.n.n/prod'); })
  6. 6. Middlewares Gergely Nemeth ● logging, auth, gzip, csrf protection, session management, etc... ● called sequentially, next() callback can trigger the next middleware //built-in middlewares app.use(express.logger()); app.use(express.compress()); app.use(express.methodOverride()); app.use(express.bodyParser()); //custom error handling app.use(function onError(err, req, res, next) { //do something with the error next(err); });
  7. 7. Routing Gergely Nemeth app.VERB(path, [callback...], callback) ● GET, POST, PUT, DELETE, PATCH ● [callback...] are middlewares app.get(/^/commits/(w+)(?:..(w+))?$/, function(req, res){ var from = req.params[0]; var to = req.params[1] || 'HEAD'; res.send('commit range ' + from + '..' + to); });
  8. 8. Sessions Gergely Nemeth ● With middlewares app.use(express.cookieParser()); app. use(express.cookieParser('some secret')); ● For authentication use: ○ Passportjs ○ Everyauth
  9. 9. Rendering Gergely Nemeth ● With template engines: Mustache(hogan), Jade, EJS... ● Layout, views, partials ● Variables are passed to the render engine app.get('/', function(req, res) { res.locals = { title: 'Title', }; return res.render( 'index', { partials: { part: 'part', } } ); });
  10. 10. Rendering #2 Gergely Nemeth ● part.html <h1>{{ title }}</h1> ● index.html {{> part}} Hello world!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×