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. PHP 关于回调的用法

    class aClass { public static function directWrite($message) { echo 'this is a static function from a ...

  2. C++11 多线程

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  3. TopFreeTheme精选免费模板【20130703】

    今天我们给大家分享13个最新的主题模板,5款WordPress主题,5款Joomla模板,3款OpenCart主题. BowThemes – BT Folio v1.0 Template for Jo ...

  4. cocos2dx 内存管理

    转载自 ocos2dx 内存管理 - 小花原创博客 - 博客频道 - CSDN.NET http://blog.csdn.net/ring0hx/article/details/7946397 coc ...

  5. openstack network

  6. tinyxml2简单使用

    引入头文件 <span style="font-size:18px;">#include "HelloWorldScene.h" #include ...

  7. Bezier曲线的原理 及 二次Bezier曲线的实现

    原文地址:http://blog.csdn.net/jimi36/article/details/7792103 Bezier曲线的原理 Bezier曲线是应用于二维图形的曲线.曲线由顶点和控制点组成 ...

  8. Unix 哲学

    1.模块原则:使用简洁的接口拼接简单的部件 2.清晰原则:清晰胜于机巧 3.组合原则:设计时考虑拼接组合 4.分离原则:策略同机制分离,接口同引擎分离. 5.简洁原则:设计要简洁,复杂度能低则低 6. ...

  9. URAL 2073 Log Files (模拟)

    题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...

  10. commondline 之三 执行jar文件

    java [-options] -jar jarfile [args...] 点击查看获取可执行jar文件方法