Node.js Express

1,658 views

Published on

Node.js Express

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,658
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
121
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Node.js Express

  1. 1. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  2. 2. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  3. 3. // Require what we need var http = require("http"); // Build the server var app = http.createServer( function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello world!"); }); // Start that server app.listen(1337, "localhost"); console.log("Server running at http://localhost:1337/");
  4. 4. var http = require("http"); http.createServer(function (req, res) { // Homepage if (req.url == "/") { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Welcome to the homepage!"); }// About page else if (req.url == "/about") { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Welcome to the about page!"); } // 404'd! else { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404 error! File not found."); } }).listen(1337, "localhost");
  5. 5. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function myFunMiddleware(request, response, next) { // Do stuff with the request and response. // When we're all done, call next() to defer // to the next middleware. next(); } Middleware Middleware Middleware
  6. 6. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var connect = require("connect"); var http = require("http"); var app = connect(); http.createServer(app).listen(1337); // Add some middleware app.use(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" response.end("Hello world!n"); }); // Add some middleware app.use(connect.logger()); app.use(connect.Security); app.use(connect.Routing); ...
  7. 7. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var express = require("express"); var http = require("http"); var app = express(); app.all("*", function (request, response, next) { response.writeHead(200, { "Content-Type": "text/plain" }); next(); }); app.get("/", function (request, response) { response.end("Welcome to the homepage!"); }); app.get("/about", function (request, response) { response.end("Welcome to the about page!"); }); app.get("*", function (request, response) { response.end("404!"); }); http.createServer(app).listen(1337);
  8. 8. app.get('/users/:id?', function (req, res, next) { var id = req.params.id; if (id) { // do something } else { next(); } }); { path: '/user/:id?', method: 'all' | 'get' | 'post' | 'put' | 'delete', callbacks: [ [Function] ], keys: [ { name: 'id', optional: true } ], regexp: /^/user(?:/([^/]+?))?/?$/i, params: [ id: '12' ] }
  9. 9. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // home.js file in Routing folder. module.exports = function (app) { // home page app.get('/', function (req, res) { res.render('index', { title: 'Home Page. ' }) }); // about page app.get('/about', function (req, res) { res.render('about', { title: 'About Me. ' }) }); } var express = require("express"); var http = require("http"); var app = express(); // Include a route file require('./routes/home')(app); http.createServer(app).listen(1337);
  10. 10. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com app.get('/users/:id?', function (req, res, next) { ... } '/:controller/:action/:id'
  11. 11. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // all environments app.configure(function() { app.set('title', 'My Application'); }); // development only app.configure('development', function() { app.set('db uri', 'localhost/dev'); }); // production only app.configure('production', function() { app.set('db uri', 'n.n.n.n/prod'); });
  12. 12. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  13. 13. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com app.use( express.bodyParser() );
  14. 14. app.put('/', function(){ console.log(req.body.user); res.redirect('back'); }); <form method="post" action="/"> <input type="hidden" name="_method" value="put" /> <input type="text" name="user[name]" /> <input type="text" name="user[email]" /> <input type="submit" value="Submit" /> </form> app.use(express.bodyParser()); app.use(express.methodOverride());
  15. 15. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function NotFound(msg) { this.name = 'NotFound'; Error.call(this, msg); Error.captureStackTrace(this, arguments.callee); } NotFound.prototype.__proto__ = Error.prototype; app.get('/404', function (req, res) { throw new NotFound; }); app.get('/500', function (req, res) { throw new Error('keyboard cat!'); }); app.error(function (err, req, res, next) { if (err instanceof NotFound) { res.render('404.jade'); } else { next(err); } });
  16. 16. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(function(err, req, res, next){ // logic }); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(logErrors); app.use(clientErrorHandler); app.use(errorHandler);
  17. 17. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com // Start Express var express = require("express"); var app = express(); // Set the view directory to /views app.set("views", __dirname + "/views"); // Let's use the Jade templating language app.set("view engine", "jade");
  18. 18. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com app.get('/', function (req, res) { res.render( 'index.jade', { title: 'My Site' } ); }); View file Model
  19. 19. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com res.render('page', { layout: 'mylayout.jade' }); partial('header', headerData );Partial View Partial View View Layout partial('footer', footerData );

×