SlideShare a Scribd company logo
1 of 34
Download to read offline
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 better — it’s actively
  maintained
       ——Alex Young from dailyjs.com
demo
• git clone https://github.com/visionmedia/
  express.git
• npm install express

• node bin/express /tmp/foo && cd /tmp/foo
• npm install -d
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 secret
here' }));
  app.use(app.router);
});

app.get('/', function(req, res){
  res.render('Hello world');
});

app.listen(3000);
Routes
function 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);
});
Cookie & Session
function 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));
  }
}
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 (!error && response.statusCode == 200) {
       sys.puts(body) // Print the google web
page.
     }
  })
Template Engines

• Haml
• Jade
• EJS
• CoffeeKup
•
Mustache

• Logic-less templates
•

•
Stache
 • Stache is mustache.js for your node
    express apps


app.set('view engine', 'mustache')
app.register(".mustache", require('stache'));
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>
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>
BUG
BUG



NodeJS
BUG



NodeJS
node
       node
node
                        node


1.   try{…} catch(error){…}
node
                             node


1.      try{…} catch(error){…}

2. process.on('uncaughtException', function
   (err) {//doSomething}
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.error(function(err, req, res){
    console.err(err.stack);
    res.render("500.html", {
      locals: {
         page_title: '500_      _i   '
      },
      status: 500
    });
  });
express                     tips
•                            try{}catch(e){}

•     routes callback     error       throw
                 error next()      express


•   uncaughtException         node

      endless world
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

More Related Content

What's hot

Hash Signaling Made Easy
Hash Signaling Made EasyHash Signaling Made Easy
Hash Signaling Made Easydavidgouldin
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JSCakra Danu Sedayu
 
Express Presentation
Express PresentationExpress Presentation
Express Presentationaaronheckmann
 
Ufo Ship for AWS ECS
Ufo Ship for AWS ECSUfo Ship for AWS ECS
Ufo Ship for AWS ECSTung Nguyen
 
Ring: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureRing: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureMark McGranaghan
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Visual Engineering
 
YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYusuke Wada
 
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
WorkFlow:  An Inquiry Into Productivity by Timothy BoltonWorkFlow:  An Inquiry Into Productivity by Timothy Bolton
WorkFlow: An Inquiry Into Productivity by Timothy BoltonMiva
 
Getting up & running with zend framework
Getting up & running with zend frameworkGetting up & running with zend framework
Getting up & running with zend frameworkSaidur Rahman
 
jQuery Foot-Gun Features
jQuery Foot-Gun FeaturesjQuery Foot-Gun Features
jQuery Foot-Gun Featuresdmethvin
 
Persistent mobile JavaScript
Persistent mobile JavaScriptPersistent mobile JavaScript
Persistent mobile JavaScriptYorick Phoenix
 
Introduction to Node.JS Express
Introduction to Node.JS ExpressIntroduction to Node.JS Express
Introduction to Node.JS ExpressEueung Mulyana
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web frameworktaggg
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...andreaslubbe
 

What's hot (20)

Express JS
Express JSExpress JS
Express JS
 
Express JS
Express JSExpress JS
Express JS
 
Hash Signaling Made Easy
Hash Signaling Made EasyHash Signaling Made Easy
Hash Signaling Made Easy
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
 
Ufo Ship for AWS ECS
Ufo Ship for AWS ECSUfo Ship for AWS ECS
Ufo Ship for AWS ECS
 
Ring: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureRing: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic Clojure
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Nodejs.meetup
Nodejs.meetupNodejs.meetup
Nodejs.meetup
 
YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービス
 
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
WorkFlow:  An Inquiry Into Productivity by Timothy BoltonWorkFlow:  An Inquiry Into Productivity by Timothy Bolton
WorkFlow: An Inquiry Into Productivity by Timothy Bolton
 
Getting up and running with Zend Framework
Getting up and running with Zend FrameworkGetting up and running with Zend Framework
Getting up and running with Zend Framework
 
Getting up & running with zend framework
Getting up & running with zend frameworkGetting up & running with zend framework
Getting up & running with zend framework
 
jQuery Foot-Gun Features
jQuery Foot-Gun FeaturesjQuery Foot-Gun Features
jQuery Foot-Gun Features
 
Persistent mobile JavaScript
Persistent mobile JavaScriptPersistent mobile JavaScript
Persistent mobile JavaScript
 
Introduction to Node.JS Express
Introduction to Node.JS ExpressIntroduction to Node.JS Express
Introduction to Node.JS Express
 
Mojolicious, real-time web framework
Mojolicious, real-time web frameworkMojolicious, real-time web framework
Mojolicious, real-time web framework
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
 

Viewers also liked

TGEG Biosphere
TGEG BiosphereTGEG Biosphere
TGEG BiosphereTGEG
 
Science and Engineering Resources: Physics and Astronomy
Science and Engineering Resources: Physics and AstronomyScience and Engineering Resources: Physics and Astronomy
Science and Engineering Resources: Physics and Astronomyguest1ecad70
 
Setmana cultural 2010
Setmana cultural 2010Setmana cultural 2010
Setmana cultural 2010guest27bce5
 
Case workshop sep06_lyd2
Case workshop sep06_lyd2Case workshop sep06_lyd2
Case workshop sep06_lyd2hjemstavn
 

Viewers also liked (8)

Code for sust_homes
Code for sust_homesCode for sust_homes
Code for sust_homes
 
TGEG Biosphere
TGEG BiosphereTGEG Biosphere
TGEG Biosphere
 
Field pre k 2010review
Field pre k 2010reviewField pre k 2010review
Field pre k 2010review
 
Un dia a l'escola 2
Un dia a l'escola 2Un dia a l'escola 2
Un dia a l'escola 2
 
Science and Engineering Resources: Physics and Astronomy
Science and Engineering Resources: Physics and AstronomyScience and Engineering Resources: Physics and Astronomy
Science and Engineering Resources: Physics and Astronomy
 
Vh sto dvd4ug
Vh sto dvd4ugVh sto dvd4ug
Vh sto dvd4ug
 
Setmana cultural 2010
Setmana cultural 2010Setmana cultural 2010
Setmana cultural 2010
 
Case workshop sep06_lyd2
Case workshop sep06_lyd2Case workshop sep06_lyd2
Case workshop sep06_lyd2
 

Similar to Build web application by express

node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascriptEldar Djafarov
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.jsdavidchubbs
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js frameworkBen Lin
 
Nodejs first class
Nodejs first classNodejs first class
Nodejs first classFin Chen
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
NodeJs
NodeJsNodeJs
NodeJsdizabl
 
Node js introduction
Node js introductionNode js introduction
Node js introductionAlex Su
 
Matthew Eernisse, NodeJs, .toster {webdev}
Matthew Eernisse, NodeJs, .toster {webdev}Matthew Eernisse, NodeJs, .toster {webdev}
Matthew Eernisse, NodeJs, .toster {webdev}.toster
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.pptWalaSidhom1
 
Future Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NETFuture Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NETGianluca Carucci
 
Developing web-apps like it's 2013
Developing web-apps like it's 2013Developing web-apps like it's 2013
Developing web-apps like it's 2013Laurent_VB
 
Going crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPGoing crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPMariano Iglesias
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineAndy McKay
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsFrancois Zaninotto
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.jsYoann Gotthilf
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gearsdion
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node jsfakedarren
 

Similar to Build web application by express (20)

node.js practical guide to serverside javascript
node.js practical guide to serverside javascriptnode.js practical guide to serverside javascript
node.js practical guide to serverside javascript
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Build Web Apps using Node.js
Build Web Apps using Node.jsBuild Web Apps using Node.js
Build Web Apps using Node.js
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
 
Nodejs first class
Nodejs first classNodejs first class
Nodejs first class
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
NodeJs
NodeJsNodeJs
NodeJs
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Matthew Eernisse, NodeJs, .toster {webdev}
Matthew Eernisse, NodeJs, .toster {webdev}Matthew Eernisse, NodeJs, .toster {webdev}
Matthew Eernisse, NodeJs, .toster {webdev}
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
 
Future Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NETFuture Decoded - Node.js per sviluppatori .NET
Future Decoded - Node.js per sviluppatori .NET
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Developing web-apps like it's 2013
Developing web-apps like it's 2013Developing web-apps like it's 2013
Developing web-apps like it's 2013
 
Going crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPGoing crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHP
 
NodeJS
NodeJSNodeJS
NodeJS
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App Engine
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gears
 
Building a real life application in node js
Building a real life application in node jsBuilding a real life application in node js
Building a real life application in node js
 

Recently uploaded

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

Build web application by express

  • 1. express Web weibo.com/mengxy www.imeigu.com
  • 3. • Web • • • •
  • 4. • Web • HTML
  • 5.
  • 6. node.js Web
  • 7. Node HTTP • cookie parser • session • •
  • 8. 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
  • 9. demo • git clone https://github.com/visionmedia/ express.git • npm install express • node bin/express /tmp/foo && cd /tmp/foo • npm install -d
  • 10. 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 secret here' })); app.use(app.router); }); app.get('/', function(req, res){ res.render('Hello world'); }); app.listen(3000);
  • 11. Routes function 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); });
  • 12. Cookie & Session function 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)); } }
  • 13. Do HTTP request • node http •
  • 14. request • git clone https://github.com/mikeal/ request.git • npm install request
  • 15. 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 web page. } })
  • 16. Template Engines • Haml • Jade • EJS • CoffeeKup •
  • 18. Stache • Stache is mustache.js for your node express apps app.set('view engine', 'mustache') app.register(".mustache", require('stache'));
  • 19. 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>
  • 20. 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>
  • 21.
  • 22. BUG
  • 25. node node
  • 26. node node 1. try{…} catch(error){…}
  • 27. node node 1. try{…} catch(error){…} 2. process.on('uncaughtException', function (err) {//doSomething}
  • 28. app.get("/err", function(req, res, next){ fs.readFile('file', function(err, data){ if (err) {throw err;} }); })
  • 29. app.get("/err", function(req, res, next){ fs.readFile('file', function(err, data){ if (err) {next(err);} }); })
  • 30. 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 }); });
  • 31. express tips • try{}catch(e){} • routes callback error throw error next() express • uncaughtException node endless world
  • 33. Forever • • Github: https://github.com/indexzero/ forever • npm install forever • forever start app.js
  • 34. Thank You! PS: We Are Hiring! weibo.com/mengxy mengxy1987@gmail.com