[Express] Level 1: First Step
Installing Express
Let's start building our new Express application by installing Express. Type the command that installs the latest version for the 4.9 branch.
npm install express@4.9
The '@' symbol to tell the npm which express version you want to install.
Locations
In our app.js, require the express module and assign it to the express variable. Using the function assigned to express, create an application instance and assign it to the app variable.
var express = require('express');
var app = express();
Using our application instance, app, create a new route that accepts GETrequests on the /locations URL path and responds with an Array of city names. The city names should be Caspiana, Indigo and Paradise.
app.get('/locations', function(request, response){
response.send(['Caspiana', 'Indigo', 'Paradise']);
});
Finally, bind our application to port 3001 and once it's ready to receive requests, print the string "Running Express" to the console.
app.listen(3001, function(){
console.log("Running Express");
});
var express = require('express');
var app = express();
app.get('/locations', function(request, response){
response.send(['Caspiana', 'Indigo', 'Paradise']);
});
app.listen(3001, function(){
console.log("Running Express");
});
Content Type I
When we run our previous code and issue a GET request to the /locations endpoint, what will the Content-Type header for the response be set to?
Answer: application/json
Content Type II
If we were to craft a response sending a string of text with the response.send()function, just like the following code, what would Express set this Content-Type to?
app.get('/locations', function(request, response) {
var cities = '<ul><li>Caspiana</li><li>Indigo</li><li>Paradise</li></ul>';
response.json(cities);
});
Answer: text/html
Cities
In order to better reflect the domain of our application, we want to change our existing route from /locations to /cities.
First, change our existing route from /locations to /cities.
Now create a new route for /locations.
Now redirect from /locations to /cities path using the Moved Permanently HTTP status code (free hint for you, the code for that is 301).
var express = require('express');
var app = express();
app.get('/cities', function (request, response) {
var cities = ['Caspiana', 'Indigo', 'Paradise'];
response.send(cities);
});
app.get('/locations', function(request, response){
response.redirect(301, '/cities');
});
app.listen(3001, function () {
console.log("Running Express");
});
[Express] Level 1: First Step的更多相关文章
- [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 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 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 2: Middleware -- 2
Logging Middleware Help finish the following middleware code in the logger.js file: On the response ...
- [Express] Level 2: Middleware -- 1
Mounting Middleware Given an application instance is set to the app variable, which of the following ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
随机推荐
- 关于python中字典的一些总结
1. 获取字典中的值,但是无异常 当在字典中取值的时候,可以使用如下两种方式: >>> d = {'name':'kel'} >>> d {'name': 'kel ...
- WebGoat学习——跨站请求伪造(Cross Site Request Forgery (CSRF))
跨站请求伪造(Cross Site Request Forgery (CSRF)) 跨站请求伪造(Cross Site Request Forgery (CSRF))也被称为:one click at ...
- MySQL DATE_SUB() 函数
定义和用法 DATE_SUB() 函数从日期减去指定的时间间隔. 语法 DATE_SUB(date,INTERVAL expr type) date 参数是合法的日期表达式.expr 参数是您希望添加 ...
- Android 订阅-发布者模式-详解
1.概念简述 Android 简称观察者模式, GoF说道:Observer模式的意图是“定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新”. 有 ...
- 两个实用的Python的装饰器
两个实用的Python的装饰器 超时函数 这个函数的作用在于可以给任意可能会hang住的函数添加超时功能,这个功能在编写外部API调用 .网络爬虫.数据库查询的时候特别有用 timeout装饰器的代码 ...
- SSO单点登录在web上的关键点 cookie跨域
概述 其实WEB单点登录的原理挺简单的,抛开那些复杂的概念,简单来讲讲如何实现一个最基本的单点登录 首先需要有两个程序 例如:http://www.site-a.com 我们简称A http://ww ...
- LeetCode(6) - ZigZag Conversion
这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...
- MapSearch 阅读随笔
MapSearch https://developer.apple.com/library/ios/samplecode/MapSearch/Introduction/Intro.html#//app ...
- 深入浅出谈存储:如何区别NAS、SAN与DAS
深入浅出谈存储:如何区别NAS.SAN与DAS 2012年02月17日16:51 来源:新浪博客 作者:林沛满 编辑:曾智强 查看全文 赞(0)评论(1) 分享 标签: DAS , SAN , 存储系 ...
- delphi 判断调试状态
DebugHook 该变量在调试状态下值为1,运行模式为0,例如,我们可以使用如下的代码来简单控制: if DebugHook=0 then