[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 ...
随机推荐
- 面试体验:Facebook 篇(转)
http://www.cnblogs.com/cathsfz/archive/2012/11/05/facebook-interview-experience.html 2012-11-05 08:2 ...
- 转 毛笔字教程ps
跟大家分享一下毛笔字怎么做出来的,主要通过字体和素材叠加,十分简单,喜欢的一起练习.做完记得交作业. 先看看最终效果: 在网上是不是经常看这些碉堡了的毛笔感觉是不是很羡慕啊,现在我就教大家怎么做出这样 ...
- TV端产品设计法则和分析
对TV端产品设计的分析太特么少了.翻遍网络,大多也是针对UI设计的分析,这篇从产品设计的角度,梳理下现有的TV端产品设计法则,顺道做点分析.(前方多图,高能预警) 目录: 1. TV端产品使用场景 2 ...
- Tcl之group arguments
1 doubel quotes This allows substitutions to occur within the quotations - or "interpolation&qu ...
- php基础知识(1)
1.判断变量是否存在isset() $v1="a"; if(!isset($v1)){ echo "变量存在"; }else{ echo "变量不存在 ...
- 第三百三十二天 how can I 坚持
今天一大早,住的这就施工了,被吵醒了.. 下午去了趟小米之家,小米5还行,黑科技不黑,哈哈. 小米5黑科技不太黑,就知道造词,整体感觉还行,就是感觉屏幕有点长,小米之家人倒是不少,还有老太太去小米之家 ...
- C++Bulder DataSnap 内存泄露元凶
DSServerClass1 DSServerClass1DestroyInstance void __fastcall TServerContainer1::DSServerClass1Destro ...
- 【转】MySQL索引和查询优化
原文链接:http://www.cnblogs.com/mailingfeng/archive/2012/09/26/2704344.html 对于任何DBMS,索引都是进行优化的最主要的因素.对于少 ...
- express 学习笔记
首先把这个库加载下来 npm install -g express 这样会安装它所有依赖包,这个非常恐怖.这个框架要依赖这么多外来的东西,如果有一个不与时俱进就会拖累整个框架的质量. C:\windo ...
- C++11空指针
[C++11空指针] 早在 1972 年,C语言诞生的初期,常数 0 带有常数及空指针的双重身分. C 使用 preprocessor macro NULL 表示空指针, 让 NULL 及 0 分别代 ...