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
590
Object Creation Pattern Performance
appltn
1
750
Introducing Mint Source
appltn
1
390
Other Decks in Technology
See All in Technology
SREとしてスタッフエンジニアを目指す / SRE Kaigi 2025
tjun
10
1.9k
MicrosoftのOSSだけでAIによるブラウザテストを構成する
ymd65536
1
160
20250122_FinJAWS
takuyay0ne
2
240
クロスアカウントな RDS Snapshot Export による カジュアルなデータ集約の仕組み / 202501-finatext-technight-lt
wa6sn
1
100
スクラムマスターの活動と組織からの期待のズレへの対応 / Dealing with the gap between Scrum Master activities and organizational expectations
pauli
1
750
Git scrapingで始める継続的なデータ追跡 / Git Scraping
ohbarye
5
620
Docker Desktop で Docker を始めよう
zembutsu
PRO
1
210
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
2
1k
Goで実践するBFP
hiroyaterui
1
130
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
330
月間60万ユーザーを抱える 個人開発サービス「Walica」の 技術スタック変遷
miyachin
3
870
横断SREの立ち上げと、AWSセキュリティへの取り組みの軌跡
rvirus0817
1
730
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
How GitHub (no longer) Works
holman
312
140k
Code Reviewing Like a Champion
maltzj
521
39k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Become a Pro
speakerdeck
PRO
26
5.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
98
18k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
A Tale of Four Properties
chriscoyier
157
23k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
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