[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 ...
随机推荐
- 应用scikit-learn做文本分类(转)
文本挖掘的paper没找到统一的benchmark,只好自己跑程序,走过路过的前辈如果知道20newsgroups或者其它好用的公共数据集的分类(最好要所有类分类结果,全部或取部分特征无所谓)麻烦留言 ...
- (转)Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- linux安装lua相关编译报错
1.报之类的错误 /usr/lib/libreadline.so: undefined reference to `PC' /usr/lib/libreadline.so: undefined ref ...
- 安装 nodejs图像处理模块 sharp
sudo npm install sharp 报错: ERROR: Please install libvips by running: brew install homebrew/science/v ...
- python oop __slots__方法
动态语言python 可以在程序运行的情况下给class加上功能.具体为 #引入一个 from types import MethodType #方法 #然后 s.set_age = MethodTy ...
- 第二百二十七天 how can I 坚持
今天去了蟒山,天池,刚去的时候身体有点难受,整天都是那样,回来就好多了,不知道怎么回事. 天池竟然是个人造池,挺大,没有去十三陵,回来都很晚了. 去天池竟然是走的小路,已经关了,不让进,里边玲玲清清的 ...
- 为什么数据科学家们选择了Python语言?
本文由 伯乐在线 - HanSir 翻译,toolate 校稿 英文出处:Quora [伯乐在线导读]:这个问题来自 Quora,题主还补充说,“似乎很多搞数据的程序员都挺擅长 Python 的,这是 ...
- quora 中有关angular与emberjs的精彩辩论
原贴地址,要注册才能看,这里只有国人翻译的一部分内容 本文源自于Quora网站的一个问题,作者称最近一直在为一个新的Rails项目寻找一个JavaScript框架,通过筛选,最终纠结于Angular. ...
- poj 2239 Selecting Courses(二分匹配简单模板)
http://poj.org/problem?id=2239 这里要处理的是构图问题p (1 <= p <= 7), q (1 <= q <= 12)分别表示第i门课在一周的第 ...
- Linux下用arptables防arp攻击
Linux下网络层防火墙iptables很强大,链路层也有类似的防火墙arptables,可针对arp地址进行限制,防止ARP网关欺骗攻击,再配合静态绑定MAC和向网关报告正确的本机MAC地址,有效解 ...