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
510
4
Share
Building web apps with Express
An introduction to the Express web framework for Node.js
Andy Appleton
February 06, 2013
More Decks by Andy Appleton
See All by Andy Appleton
Done is better than perfect
appltn
0
600
Rage against the state machine
appltn
1
540
Modular UI with (Angular || Ember)
appltn
0
130
The Modern JavaScript Application
appltn
5
680
Object Creation Pattern Performance
appltn
1
830
Introducing Mint Source
appltn
1
410
Other Decks in Technology
See All in Technology
AI時代のIssue駆動開発のススメ
moongift
PRO
0
330
SaaSの操作主体は人間からAIへ - 経理AIエージェントが目指す深い自動化
nishihira
0
130
15年メンテしてきたdotfilesから開発トレンドを振り返る 2011 - 2026
giginet
PRO
2
250
GitHub Actions侵害 — 相次ぐ事例を振り返り、次なる脅威に備える
flatt_security
12
7k
PostgreSQL 18のNOT ENFORCEDな制約とDEFERRABLEの関係
yahonda
0
150
タスク管理も1on1も、もう「管理」じゃない - KiroとBedrock AgentCoreで変わった“判断の仕事”
yusukeshimizu
0
150
会社紹介資料 / Sansan Company Profile
sansan33
PRO
16
410k
第26回FA設備技術勉強会 - Claude/Claude_codeでデータ分析 -
happysamurai294
0
270
Oracle Cloud Infrastructure(OCI):Onboarding Session(はじめてのOCI/Oracle Supportご利⽤ガイド)
oracle4engineer
PRO
2
17k
OCI技術資料 : ロード・バランサ 概要 - FLB・NLB共通
ocise
4
27k
脳が溶けた話 / Melted Brain
keisuke69
1
1.1k
Sansanの認証基盤を支えるアーキテクチャとその振り返り
sansantech
PRO
1
120
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
94
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
[SF Ruby Conf 2025] Rails X
palkan
2
880
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
200
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
140
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
330
GitHub's CSS Performance
jonrohan
1032
470k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
160
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.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