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的更多相关文章

  1. [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 ...

  2. [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 ...

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

    Response Body What would the response body be set to on a DELETE request to /cities/DoesNotExist ? H ...

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

    Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...

  5. [Express] Level 3: Massaging User Data

    Flexible Routes Our current route only works when the city name argument matches exactly the propert ...

  6. [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 ...

  7. [Express] Level 2: Middleware -- 2

    Logging Middleware Help finish the following middleware code in the logger.js file: On the response  ...

  8. [Express] Level 2: Middleware -- 1

    Mounting Middleware Given an application instance is set to the app variable, which of the following ...

  9. 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 ...

随机推荐

  1. MySQL_PHP学习笔记_2015_0614_PHP传参总结_URL传参_表单传参

    1. PHP 传参总结   1.1 url 传参     解析方法(下面两种解读方式均可以): $firstName1 = $_GET['firstName']; $firstName2 = $_RE ...

  2. 约瑟夫环 --- 面向对象 --- java代码

    约瑟夫环 的 面向对象 解法 罗马人占领乔塔帕特后,39个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个 ...

  3. 【LeetCode】96 - Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  4. Linux中的.emacs文件

    刚开始的时候在Windows下使用emacs,那个时候配置 .emacs文件直接去C盘里\Users\(username)\AppData\Roaming 路径下查找就可以了(最开始的时候可以打开em ...

  5. 渗透测试实例Windows XP SP2

    一.msf> use exploit/windows/dcerpc/ms03_026_dcom.看到命令提示符的改变表明该命令已经运行成功. 二.为漏洞利用代码设置必要的参数,show opti ...

  6. Vi的基本使用方法

    转载自http://linux.chinaunix.net/doc/office/2005-01-24/898.shtml vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何 ...

  7. C++的引用类型的变量到底占不占用内存空间?

    ——by  karottc 分析一下 C++ 里面的引用类型(例如: int &r = a;  )中的 r 变量是否占用内存空间呢?是否和  int *p = &a;  中的 p 变量 ...

  8. 在Cubieboard上关闭irqbalance服务避免内存泄漏

    十一一个假期回来,顺手看了看自己的cubieboard运行状态怎么样 aria2正常: btsync正常: samba正常: 很好, 顺手htop一下,已经开机了13天了,CPU使用率4%,内存使用率 ...

  9. geeksforgeeks@ Minimum Points To Reach Destination (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=91 Minimum Points To Reach Destination Gi ...

  10. How to install php evn on ubuntu

    1. How to install PHP EVN 打开终端,也就是命令提示符. 我们先来最小化组建安装,按照自己的需求一步一步装其他扩展.命令提示符输入如下命令: 1 sudo apt-get in ...