the root directory from which to serve static assets Example middlewares contd

The root directory from which to serve static assets

This preview shows page 19 - 25 out of 37 pages.

the root directory from which to serve static assets Example middlewares (cont’d)
Image of page 19
var express = require ( 'express' ) var app = express () var cookieParser = require ( 'cookie-parser' ) // load the cookie-parsing middleware app . use ( cookieParser ()) An Express application is essentially a series of middleware calls! Third-party middleware: Example middlewares (cont’d)
Image of page 20
An Express app serving static files in a directory var express = require ( 'express' ); var app = express (); app . use ( express.static ( 'public' )); var server = app . listen ( 8081 , function () { var host = server . address (). address var port = server . address (). port console . log ( "Example app listening at : %s" , host , port ) }) Pass the name of the directory , which is to be marked as the location of static files, to the express.static middleware Then the files in the directory can be retrieved directly, such as: app.js
Image of page 21
var express = require ( 'express' ); var app = express (); app . get ( '/index.html' , function ( req , res ) { res . sendFile ( __dirname + "/" + "index.html" ); }) app . get ( '/process_get' , function ( req , res ) { // Prepare output in JSON format response = { first_name : req . query . first_name , last_name : req . query . last_name }; console . log ( response ); res . json ( response ); }) var server = app . listen ( 8081 , function () { var host = server . address (). address var port = server . address (). port console . log ( "Example app listening at " , host , port ) }) the directory in which the currently executing script resides An Express app serving static file and handling form data sent by GET <html> <body> <form action = "" method = "GET" > First Name: <input type = "text" name = "first_name" > <br> Last Name: <input type = "text" name = “last_name" > <br> <input type = "submit" value = "Submit" > </form> </body> </html> c Wu send response in JSON app.js l l index.html
Image of page 22
var express = require ( 'express' ); var app = express (); var bodyParser = require ( 'body-parser' ); // Create application/x-www-form-urlencoded parser var urlencodedParser = bodyParser . urlencoded ({ extended : false }) app . get ( '/index.html' , function ( req , res ) { res . sendFile ( __dirname + "/" + "index.html" ); }) app . post ( '/process_post' , urlencodedParser , function ( req , res ) { // Prepare output in JSON format response = { first_name : req . body . first_name , last_name : req . body . last_name }; console . log ( response ); res . json ( response ); }) var server = app . listen ( 8081 , function () { var host = server . address (). address var port = server . address (). port console . log ( "Example app listening at " , host , port ) }) load body-parser module An Express app serving static file and handling form data sent by POST app.js a middleware that parses the request body as URL encoded data and exposes the resulting object (containing the keys and values) on req . body The extended option allows to choose between parsing the URL- encoded data with the querystring library (when false) or the qs library (when true)
Image of page 23
var express = require ( 'express' ); var app = express (); var bodyParser = require ( 'body-parser' ); // Create application/x-www-form-urlencoded parser var urlencodedParser = bodyParser . urlencoded ({ extended : false }) app . get ( '/index.html' , function ( req , res ) { res . sendFile ( __dirname + "/" + "index.html" ); }) app . post ( '/process_post' , urlencodedParser , function ( req , res ) {
Image of page 24

Want to read all 37 pages?

Image of page 25

Want to read all 37 pages?

You've reached the end of your free preview.

Want to read all 37 pages?

  • Fall '13
  • Dr. C. Wu
  • Ajax

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture