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
460
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
470
Rage against the state machine
appltn
1
380
Modular UI with (Angular || Ember)
appltn
0
100
The Modern JavaScript Application
appltn
5
530
Object Creation Pattern Performance
appltn
1
660
Introducing Mint Source
appltn
1
380
Other Decks in Technology
See All in Technology
データ品質をコード化! LINEヤフーのMLOpsを最適化する "ACP Data Quality" の紹介
lycorptech_jp
PRO
3
290
.NETの非同期戦略とUnityとの相互運用
neuecc
2
2.4k
統計的学習理論読み Chapter 1
kmatsui
4
1k
検証からプロダクトへ: シームレスなLLM開発の ためのしくみ作り
nunukim
1
210
2023 Japan AWS Jr.Championsに選出されての振り返りとこれから
hiropy877
1
130
サービス成長と共に肥大化するモノレポ、長くなるCI時間 / As services grow, monorepos get bigger and CI time gets longer
kohbis
5
2.1k
バッチ処理のSLOをどう設計するか
rynsuke
7
590
どう買う?Azure
kuniteru
1
190
社内共通ルールを値オブジェクトにして社内ライブラリとして運用してみた話
leveragestech
2
1.2k
Why do you get AWS certificates
hirosys
0
120
AFTを運用していたらAWS Configの課金が急増していた件
msato
0
120
これまでのキャリアとこれからMLエンジニアとしてどう動くか
masatakashiwagi
1
790
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
319
23k
10 Git Anti Patterns You Should be Aware of
lemiorhan
644
57k
How to name files
jennybc
62
92k
Documentation Writing (for coders)
carmenintech
59
3.8k
Ruby is Unlike a Banana
tanoku
95
10k
jQuery: Nuts, Bolts and Bling
dougneiner
57
7.1k
Large-scale JavaScript Application Architecture
addyosmani
501
110k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
8
8.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
14
2.6k
Git: the NoSQL Database
bkeepers
PRO
421
63k
What's in a price? How to price your products and services
michaelherold
236
11k
A Philosophy of Restraint
colly
195
15k
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