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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Andy Appleton
February 06, 2013
Technology
4
500
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
590
Rage against the state machine
appltn
1
520
Modular UI with (Angular || Ember)
appltn
0
120
The Modern JavaScript Application
appltn
5
660
Object Creation Pattern Performance
appltn
1
820
Introducing Mint Source
appltn
1
410
Other Decks in Technology
See All in Technology
レガシー共有バッチ基盤への挑戦 - SREドリブンなリアーキテクチャリングの取り組み
tatsukoni
0
200
usermode linux without MMU - fosdem2026 kernel devroom
thehajime
0
210
コスト削減から「セキュリティと利便性」を担うプラットフォームへ
sansantech
PRO
3
1.2k
仕様書駆動AI開発の実践: Issue→Skill→PRテンプレで 再現性を作る
knishioka
2
570
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
日本語テキストと音楽の対照学習の技術とその応用
lycorptech_jp
PRO
1
420
Databricks Free Edition講座 データサイエンス編
taka_aki
0
290
What happened to RubyGems and what can we learn?
mikemcquaid
0
240
Amazon Bedrock AgentCore 認証・認可入門
hironobuiga
2
500
Amazon S3 Vectorsを使って資格勉強用AIエージェントを構築してみた
usanchuu
3
430
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
3.8k
今日から始めるAmazon Bedrock AgentCore
har1101
4
380
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
170
Context Engineering - Making Every Token Count
addyosmani
9
650
Marketing to machines
jonoalderson
1
4.6k
Speed Design
sergeychernyshev
33
1.5k
What's in a price? How to price your products and services
michaelherold
247
13k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
150
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
250
Site-Speed That Sticks
csswizardry
13
1.1k
HDC tutorial
michielstock
1
350
How to build a perfect <img>
jonoalderson
1
4.9k
Bash Introduction
62gerente
615
210k
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