[Express] Level 2: Middleware -- 1

Mounting Middleware

Given an application instance is set to the app variable, which of the following function calls would you use to mount a middleware called logger ?

Answer:

app.use(logger);

Default Middleware

What is the only middleware that‘s shipped with Express 4?

Answer: express-static

Express Static

Change the code in app.js to use the express-static middleware instead of the response.sendFile() function.

var express = require(‘express‘);
var app = express();

app.get(‘/‘, function (request, response) {
  response.sendFile(__dirname + ‘/public/index.html‘);
});

app.get(‘/cities‘, function(req, res){
  var cities = [‘Lotopia‘, ‘Caspiana‘, ‘Indigo‘];
  res.send(cities);
});

app.listen(3001);

Remove our app.get() containing the root ‘/‘ route.

Mount the static middleware and serve files under the public directory.

Answer:

var express = require(‘express‘);
var app = express();

/*app.get(‘/‘, function (request, response) {
  response.sendFile(__dirname + ‘/public/index.html‘);
});*/

//cd public
app.use(express.static(‘public‘));

app.get(‘/cities‘, function(req, res){
  var cities = [‘Lotopia‘, ‘Caspiana‘, ‘Indigo‘];
  res.send(cities);
});

app.listen(3001);

Script Tags

Now we can add some client side javascript by including thejquery.js and client.js files.

Within index.html, include jquery.js using a <script> tag.

Within index.html, include client.js using a <script> tag.

Now in the client.js file, complete the code for the $.get function so that it calls the /cities URL path, and then runs the appendToList function.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Cities</title>
</head>
<body>
  <h1>Cities</h1>

  <ul class=‘city-list‘></ul>
   <script src="jquery.js"></script>
   <script src="client.js"></script>
</body>
</html>

Now in the client.js file, complete the code for the $.get function so that it calls the /cities URL path, and then runs the appendToList function.

$(function(){

  $.get(‘/cities‘, appendToList ); 

  function appendToList(cities) {
    var list = [];
    for(var i in cities){
      list.push($(‘<li>‘, { text: cities[i] }));
    }
    $(‘.city-list‘).append(list);
  }
});
时间: 2024-10-12 22:21:15

[Express] Level 2: Middleware -- 1的相关文章

[Express] Level 2: Middleware -- 2

Logging Middleware Help finish the following middleware code in the logger.js file: On the response object, listen to the event that's emitted when the response has been handed off from Express to the underlying Operating System. response.on('finish'

[Express] Level 3: Massaging User Data

Flexible Routes Our current route only works when the city name argument matches exactly the properties in the cities object. This is a problem. We need a way to make our code more flexible. Inside our route, call the parseCityName() function passing

[Express] Level 4: Body-parser -- Post

Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express application. npm install body-parser Require the body-parser npm module and assign it to a variable calledbodyParser. var bodyParser = require('body-parser'

[Express] Level 5: Route Instance -- refactor the code

Route Instance Let's rewrite our cities routes using a Route Instance. Create a new Route Instance for the '/cities' URL path and assign it to thecitiesRoute variable. var citiesRoute = app.route('/cities'); Move the code from our previous app.get() 

[Express] Level 5: Route file

Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and assign it to the router variable. var router = express.Router(); When we are done, our router will be mounted on the /cities path. With this in mind,

[Express] Level 3: Reading from the URL

City Search We want to create an endpoint that we can use to filter cities. Follow the tasks below to to create this new route. Create a new route for GET request to '/cities'. The second argument should be a callback function which takes request and

[Express] Level 4: Body-parser -- Delete

Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? Here's the link to the sendStatus function source code if you need to take a look. Answer: 404 Delete Route Create a Dynamic Route for deleting cities

[Node.js] Serve Static Files with Express

In this lesson we will find out how to serve static assets (images, css, stylesheets, etc.) with Express. We will go over writing initial boilerplate for a simple Express app, and using Express's built-in middleware to serve these assets through a we

透析Express.js

前言 最近,本屌在试用Node.js,在寻找靠谱web框架时发现了Express.js.Express.js在Node.js社区中是比较出名web框架,而它的定位是“minimal and flexible(简洁.灵活)”. 进击的Express.js 1. 底层的Http module Node有Http module,本质上,我们可以直接通过他写Web应用.Http module使用很简单: //////////////// // app.js //////////////// // 加载所