express        Web     weibo.com/mengxy      www.imeigu.com
• • •
•    •   Web    •    •    •    •
•    •   Web    •   HTML
node.js   Web
Node HTTP•      cookie parser•      session••
Express• Express by TJ Holowaychuk is an extremely  popular web framework. It’s fast, simple, and  easy to learn, and even...
demo• git clone https://github.com/visionmedia/  express.git• npm install express• node bin/express /tmp/foo && cd /tmp/fo...
var express = require(express);var app = express.createServer();app.configure(function(){  app.use(express.bodyParser()); ...
Routesfunction loadUser(req, res, next) {  var user = users[req.params.id];  if (user) {    req.user = user;    next();  }...
Cookie & Sessionfunction loadUser(req, res, next) {  if (req.session.user) {    return req.session.user;  }  var user = us...
Do HTTP request•     node http•
request• git clone https://github.com/mikeal/  request.git• npm install request
var request = require(request);  request(     {uri:http://www.google.com},     function (error, response, body) {     if (...
Template Engines• Haml• Jade• EJS• CoffeeKup•
Mustache• Logic-less templates••
Stache • Stache is mustache.js for your node    express appsapp.set(view engine, mustache)app.register(".mustache", requir...
app.get(/, function (req, res) {  res.render("index", {    locals: {       title: "Title content",       body: "Page conte...
app.get(/, function (req, res) {  res.render("index", {    locals: {       title: "Title content",       body: "Page conte...
BUG
BUGNodeJS
BUGNodeJS
node       node
node                        node1.   try{…} catch(error){…}
node                             node1.      try{…} catch(error){…}2. process.on(uncaughtException, function   (err) {//do...
app.get("/err", function(req, res, next){   fs.readFile(file, function(err, data){      if (err) {throw err;}   });})
app.get("/err", function(req, res, next){   fs.readFile(file, function(err, data){      if (err) {next(err);}   });})
app.get("/err", function(req, res, next){   fs.readFile(file, function(err, data){      if (err) {next(err);}   });})app.e...
express                     tips•                            try{}catch(e){}•     routes callback     error       throw   ...
One more tool
Forever•• Github: https://github.com/indexzero/  forever• npm install forever• forever start app.js
Thank You!PS: We Are Hiring!             weibo.com/mengxy           mengxy1987@gmail.com
Build web application by express
Build web application by express
Upcoming SlideShare
Loading in...5
×

Build web application by express

2,059

Published on

Published in: Technology, Design
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,059
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
65
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Build web application by express

  1. 1. express Web weibo.com/mengxy www.imeigu.com
  2. 2. • • •
  3. 3. • • Web • • • •
  4. 4. • • Web • HTML
  5. 5. node.js Web
  6. 6. Node HTTP• cookie parser• session••
  7. 7. Express• Express by TJ Holowaychuk is an extremely popular web framework. It’s fast, simple, and easy to learn, and even better — it’s actively maintained ——Alex Young from dailyjs.com
  8. 8. demo• git clone https://github.com/visionmedia/ express.git• npm install express• node bin/express /tmp/foo && cd /tmp/foo• npm install -d
  9. 9. var express = require(express);var app = express.createServer();app.configure(function(){ app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret: your secrethere })); app.use(app.router);});app.get(/, function(req, res){ res.render(Hello world);});app.listen(3000);
  10. 10. Routesfunction loadUser(req, res, next) { var user = users[req.params.id]; if (user) { req.user = user; next(); } else { next(new Error(Failed to load user +req.params.id)); }}app.get(/user/:id, loadUser, function(req, res){ res.send(Viewing user + req.user.name);});
  11. 11. Cookie & Sessionfunction loadUser(req, res, next) { if (req.session.user) { return req.session.user; } var user = users[req.params.id]; if (user) { req.session.user = req.user = user; next(); } else { next(new Error(Failed to load user +req.params.id)); }}
  12. 12. Do HTTP request• node http•
  13. 13. request• git clone https://github.com/mikeal/ request.git• npm install request
  14. 14. var request = require(request); request( {uri:http://www.google.com}, function (error, response, body) { if (!error && response.statusCode == 200) { sys.puts(body) // Print the google webpage. } })
  15. 15. Template Engines• Haml• Jade• EJS• CoffeeKup•
  16. 16. Mustache• Logic-less templates••
  17. 17. Stache • Stache is mustache.js for your node express appsapp.set(view engine, mustache)app.register(".mustache", require(stache));
  18. 18. app.get(/, function (req, res) { res.render("index", { locals: { title: "Title content", body: "Page content" }, partials: { heading: <title>{{title}}</title> } });});<!-- index.mustache --><html><head> {{>heading}} </head><body> {{{body}}} </body></html>
  19. 19. app.get(/, function (req, res) { res.render("index", { locals: { title: "Title content", body: "Page content" }, partials: { heading: <title>{{title}}</title> } });}); <html> <head><!-- index.mustache --> <title>Title content</title><html> </head><head> {{>heading}} </head> <body> Page content </body><body> {{{body}}} </body> </html></html>
  20. 20. BUG
  21. 21. BUGNodeJS
  22. 22. BUGNodeJS
  23. 23. node node
  24. 24. node node1. try{…} catch(error){…}
  25. 25. node node1. try{…} catch(error){…}2. process.on(uncaughtException, function (err) {//doSomething}
  26. 26. app.get("/err", function(req, res, next){ fs.readFile(file, function(err, data){ if (err) {throw err;} });})
  27. 27. app.get("/err", function(req, res, next){ fs.readFile(file, function(err, data){ if (err) {next(err);} });})
  28. 28. app.get("/err", function(req, res, next){ fs.readFile(file, function(err, data){ if (err) {next(err);} });})app.error(function(err, req, res){ console.err(err.stack); res.render("500.html", { locals: { page_title: 500_ _i }, status: 500 }); });
  29. 29. express tips• try{}catch(e){}• routes callback error throw error next() express• uncaughtException node endless world
  30. 30. One more tool
  31. 31. Forever•• Github: https://github.com/indexzero/ forever• npm install forever• forever start app.js
  32. 32. Thank You!PS: We Are Hiring! weibo.com/mengxy mengxy1987@gmail.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×