[Express] Level 1: First Step

时间:2022-11-08 20:03:23

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. &lbrack;Express&rsqb; Level 5&colon; Route file

    Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and ...

  2. &lbrack;Express&rsqb; Level 5&colon; Route Instance -- refactor the code

    Route Instance Let's rewrite our cities routes using a Route Instance. Create a new Route Instance f ...

  3. &lbrack;Express&rsqb; Level 4&colon; Body-parser -- Delete

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

  4. &lbrack;Express&rsqb; Level 4&colon; Body-parser -- Post

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

  5. &lbrack;Express&rsqb; Level 3&colon; Massaging User Data

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

  6. &lbrack;Express&rsqb; Level 3&colon; 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. &lbrack;Express&rsqb; Level 2&colon; Middleware -- 2

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

  8. &lbrack;Express&rsqb; Level 2&colon; Middleware -- 1

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

  9. 1064&period; Complete Binary Search Tree &lpar;30&rpar;【二叉树】——PAT &lpar;Advanced Level&rpar; Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

随机推荐

  1. 使用github pages, hexo搭建个人博客教程

    具体的原理性的东西就不说了直接上教程,怕等下自己忘了. 一. github 阶段 申请一个github 账号并成功登录进去. 创建一个名字为xxx.github.io的空项目. 二. hexo 阶段 ...

  2. Xcode报错:&OpenCurlyDoubleQuote;Your build settings specify a provisioning profile with the UUID&period;&period;&period;&period;&period; however&comma; no such provisioning profile was found”

    运行环境: Xcode5 & 5.0及以上版本 对工程进行Archive打包的时候出现如下错误   问题描述: Code Sign error: No matching provisionin ...

  3. 苹果MAC中安装并搭建Android开发环境的详细步骤

    Android的开发平台搭建主要需要的工具有:Java虚拟机JDK.Eclipse.Eclipse插件ADT(Android Developer Tool)和Android开发包SDK,以下是具体的安 ...

  4. elmah - Error Logging Modules and Handlers for ASP&period;NET - 1 : 初体验

    elmah(英文):https://code.google.com/p/elmah/   写作思路:先看结果,然后再说原理   elmah文章基本内容如下   1.安装 2.基本使用 3.详细配置讲解 ...

  5. (IOS)关于Xcode的架构&lpar;Architectures&rpar;设置

    首先来了解一下Architectures中几个参数的含义 ARMv6:ARM11内核用于iPhone2G和iPhone3G中的架构 ARMv7:modern ARM内核用于iPhone3GS和iPho ...

  6. loadrunner入门篇-Vuser发生器

    Vuser 发生器(Visual User Generator,VuGen),主要通过捕获客户端向服务器发送的HTTP请求,将这些请求录制成脚本,在回放时将捕获的HTTP请求再次发送,以达到模拟客户行 ...

  7. VDSR

    提出SRCNN问题 context未充分利用 Convergence 慢 Scale Factor 训练指定fator的模型再重新训练其他fator的模型低效 context 对于更大的scale-f ...

  8. &lbrack;C&num; 基础知识系列&rsqb;专题七&colon; 泛型深入理解&lpar;一&rpar; (转载)

    引言: 在上一个专题中介绍了C#2.0 中引入泛型的原因以及有了泛型后所带来的好处,然而上一专题相当于是介绍了泛型的一些基本知识的,对于泛型的性能为什么会比非泛型的性能高却没有给出理由,所以在这个专题 ...

  9. python-解释器模式

    源码地址:https://github.com/weilanhanf/PythonDesignPatterns 说明: 解释器模式在面向对象语言实现的编译器中得到了广泛的应用.但是此模式进适用于建大的 ...

  10. 电子商务&lpar;电销&rpar;平台中内容模块&lpar;Content&rpar;数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 文章表 (article)|-- 自动编号|-- 文章标题 (title)|-- 文章类别编号 (c ...