100%(1)1 out of 1 people found this document helpful
This preview shows page 19 - 25 out of 37 pages.
the root directory from which to serve static assetsExample middlewares (cont’d)
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)
An Express app serving static files in a directoryvar 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
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 residesAn 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>cWusend response in JSONapp.jsllindex.html
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 moduleAn Express app serving static file and handling form data sent by POSTapp.jsa middleware that parses the request body as URL encoded data and exposes the resulting object (containing the keys and values) on req.bodyThe extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true)
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) {