博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Express] Level 1: First Step
阅读量:7028 次
发布时间:2019-06-28

本文共 2496 字,大约阅读时间需要 8 分钟。

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 = '
  • Caspiana
  • Indigo
  • Paradise
'; 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");});

 

转载地址:http://icrxl.baihongyu.com/

你可能感兴趣的文章
串口DTU设备常见问题处理
查看>>
28.umask值
查看>>
文件操作工具类
查看>>
nginx教程从入门到精通(ttlsa出品)
查看>>
squid日志之access.log格式+内容
查看>>
我的友情链接
查看>>
LVS NAT 模式突然很卡ip_conntrack
查看>>
重拾CCNA,学习笔记持续更新ing......(7)
查看>>
FreeBSD下的开机自启动
查看>>
我的友情链接
查看>>
Linux命令行快捷键
查看>>
python 的实用技巧
查看>>
创建RHCS集群环境
查看>>
电子商务未来的趋势,难道我真的错了?
查看>>
工厂方法模式
查看>>
360安全卫士怎么登录问题
查看>>
linux下的DNS缓存服务
查看>>
实现一键分享的代码
查看>>
详解Linux运维工程师必备技能
查看>>
[20181109]12c sqlplus rowprefetch参数5
查看>>