Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building web apps with Express
Search
Andy Appleton
February 06, 2013
Technology
4
480
Building web apps with Express
An introduction to the Express web framework for Node.js
Andy Appleton
February 06, 2013
Tweet
Share
More Decks by Andy Appleton
See All by Andy Appleton
Done is better than perfect
appltn
0
550
Rage against the state machine
appltn
1
450
Modular UI with (Angular || Ember)
appltn
0
110
The Modern JavaScript Application
appltn
5
600
Object Creation Pattern Performance
appltn
1
750
Introducing Mint Source
appltn
1
390
Other Decks in Technology
See All in Technology
[2025-02-07]生成AIで変える問い合わせの未来 〜チームグローバル化の香りを添えて〜
tosite
1
290
関東Kaggler会LT: 人狼コンペとLLM量子化について
nejumi
3
460
WAF に頼りすぎない AWS WAF 運用術 meguro sec #1
izzii
0
460
事業継続を支える自動テストの考え方
tsuemura
0
300
7日間でハッキングをはじめる本をはじめてみませんか?_ITエンジニア本大賞2025
nomizone
2
1.4k
オブザーバビリティの観点でみるAWS / AWS from observability perspective
ymotongpoo
7
1k
SA Night #2 FinatextのSA思想/SA Night #2 Finatext session
satoshiimai
1
100
Building Products in the LLM Era
ymatsuwitter
10
4.4k
組織貢献をするフリーランスエンジニアという生き方
n_takehata
1
1k
生成AIの利活用を加速させるための取り組み「prAIrie-dog」/ Shibuya_AI_1
visional_engineering_and_design
1
140
RSNA2024振り返り
nanachi
0
500
AndroidデバイスにFTPサーバを建立する
e10dokup
0
240
Featured
See All Featured
Navigating Team Friction
lara
183
15k
Designing Experiences People Love
moore
139
23k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
Writing Fast Ruby
sferik
628
61k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
BBQ
matthewcrist
86
9.5k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
31
2.1k
Transcript
Building web apps with Express Andy Appleton @appltn http://appleton.me
Express is a simple web application framework http://expressjs.com/
...built on Connect, the middleware framework http://www.senchalabs.org/connect/
Connect provides a bunch of handy utilities for dealing with
HTTP requests
var app = connect() .use(connect.logger('dev')) .use(connect.static('public')) .use(function(req, res){ res.end('hello world\n');
}) .listen(3000);
But anyway, Express
$ npm install -g express Install express globally
$ npm install -g express Init a new express app
in ./awesome-demo $ express awesome-demo
$ npm install -g express Install the app’s dependencies with
npm $ express awesome-demo $ cd awesome-demo && npm install
$ npm install -g express Run it! $ express awesome-demo
$ cd awesome-demo && npm install $ node app.js >> Express server listening on port 3000
None
None
var express = require('express'); ... var app = express();
// Get & set an app property app.set('name', 'value'); //
Use a middleware function app.use(myMiddlewareFunction()); // Respond to an HTTP request app.get('/path', callbackFn); app.post('/path', callbackFn); app.put('/path', callbackFn); // ...etc
Routing
app.get('/', routes.index); app.get('/users', routes.users.index); app.get('/users/:id', routes.users.show); app.post('/users/:id', routes.users.create); app.put('/users/:id', routes.users.update);
Handling a route // /users routes.index = function(req, res) {
res.send('Hello Bath'); }; // /users/:id routes.users.show = function(req, res) { var userId = req.params.id; res.send('Your userId is ' + userId); };
Rendering HTML templates
Rendering HTML templates routes.index = function(req, res) { res.render('index'); };
routes.users.show = function(req, res) { var userId = req.params.id; res.render('users/show', { id: userId }); };
doctype 5 html head ... body block content extends layout
block content h1= title p Welcome to #{title} ./views/layout.jade ./views/index.jade
"dependencies": { ... "hbs": "*" } ./package.json $ npm install
./app.js app.configure(function(){ ... app.set('view engine', 'hbs'); ... }); app.configure(function(){ ... app.set('view engine', 'jade'); ... });
<!DOCTYPE html> <html> <head>...</head> <body> {{{body}}} </body> </html> ./views/layout.hbs ./views/index.hbs
<h1>{{title}}</h1> <p>Welcome to {{title}}</p>
routes.index = function(req, res) { res.render('index'); }; routes.users.show = function(req,
res) { var userId = req.params.id; res.render('users/show', { id: userId }); };
Sessions
// must come before router app.use(express.cookieParser('secret')); app.use(express.session()); app.use(app.router); Session support
is middleware
routes.users.show = function(req, res) { req.session.id || (req.session.id = 1);
res.render('users/show', { id: req.session.id }); };
Andy Appleton @appltn http://appleton.me