[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);
}
});
[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 ...
- [Express] Level 4: Body-parser -- Post
Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- [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 ...
- [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 f ...
- [Express] Level 4: Body-parser -- Delete
Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? H ...
- [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 t ...
- [Express] Level 1: First Step
Installing Express Let's start building our new Express application by installing Express. Type the ...
- 透析Express.js
前言 最近,本屌在试用Node.js,在寻找靠谱web框架时发现了Express.js.Express.js在Node.js社区中是比较出名web框架,而它的定位是“minimal and flexi ...
随机推荐
- DouNet学习_Excel导入导出
Excel --->列是有限的 -->数据靠在单元格右边是数字类型,左边是字符串类型 把一个数字当初字符串来显示 在前面加个 ' -->用程序操作Excel 可以使用Excel的所有 ...
- STL源码剖析读书笔记--第四章--序列式容器
1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...
- javascript里面的闭包,作用域,预解析
函数的作用域 1.全局变量=公用卫生间 2.局部变量=次卧卫生间 局部变量 全局无法使用 局部声明变量不加var的话就变成全局变量(不推荐使用) 3.闭包=次卧的可以用自己的卫生间 ...
- 2010“架构师接龙”问答--杨卫华VS赵劼(转)
add by zhj:虽然是几年前的文章,但还是很有参考价值的 原文:http://blog.zhaojie.me/2010/05/programmer-magazine-2010-5-archite ...
- Attach source code to a Netbeans Library Wrapper Module
http://rubenlaguna.com/wp/2008/02/22/attach-source-code-to-a-netbeans-library-wrapper-module/ Attach ...
- C++11外部模板
[C++11之外部模板] 在标准C++中,只要在编译单元内遇到被完整定义的模板,编译器都必须将其实例化(instantiate).这会大大增加编译时间,特别是模板在许多编译单元内使用相同的参数实例化. ...
- Codeforces 583D. Once Again... (LIS变形)
题目链接:http://codeforces.com/contest/583/problem/D 给你t个长度为n的数组.问你最长不下降子序列的长度. 一开始用第一个n数组的lis和最后一个n数组的l ...
- HDU3466Proud Merchants(贪心&背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有 ...
- UVaLive 6608 Cabin Baggage (水题)
题意:给定四个数代表长宽高和重,问你是不是满足下面条件,长不高于56,宽不宽于45,高不高于25,或者总和不大于125,并且重量不高于7. 析:判断输出就好,注意这个题是或,不要想错了. 代码如下: ...
- 手把手教你玩转SOCKET模型之重叠I/O篇(上)
“身为一个初学者,时常能体味到初学者入门的艰辛,所以总是想抽空作点什么来尽我所能的帮助那些需要帮助的人.我也希望大家能把自己的所学和他人一起分享,不要去鄙视别人索取时的贪婪,因为最应该被鄙视的是不肯付 ...