[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 ...
随机推荐
- 浅谈AndroidManifest.xml与R.java及各个目录的作用
在开发Android项目中,AndroidManifest.xml与R.java是自动生成的.但是对于测试来说,非常重要.经过师父的点拨,我对AndroidManifest.xml与R.java有了更 ...
- java 代码如何生成 chm
由于要把一个框架的东西打成 chm, 今天在网上找了几篇文章 http://blog.sina.com.cn/s/blog_5d31611a0100gqwp.html 李顺利 首先第一步,从eclip ...
- 使用curl操作openstack swift
openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/con ...
- 动手动脑之查看String.equals()方法的实现代码及解释
动手动脑 请查看String.equals()方法的实现代码,注意学习其实现方法. 第一个是false,后三个是true. package stringtest; public class Strin ...
- php框架推荐
ThinkPHP, 国内开发的框架,特别容易入门,中文文档细致,表述准确. Laravel, 国外框架,非常高级的一个框架,特别是前端比较模块化,但入门难一些,速度不高. laravel在lampp ...
- 第三百五十六天 how can I 坚持
一年了,三百五十六天.写个算法算下对不对. 今天突然想买辆自行车了.云马智行车,还是捷安特,好想买一辆. 网好卡.貌似少记了一天呢,357了.好快. 睡觉了,还没锻炼呢,太晚了. 1458748800 ...
- 转】MyEclipse使用总结——使用MyEclipse打包带源码的jar包
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4136303.html 感谢! 平时开发中,我们喜欢将一些类打包成jar包,然后在别的项目中继续使用,不过由于看不 ...
- js闭包测试
本文的诞生,源自近期打算做的一个关于javascript中的闭包的专题,由于需要解析闭包对垃圾回收的影响,特此针对不同的javascript引擎,做了相关的测试. 为了能从本文中得到需要的知识,看本文 ...
- HTML5每日一练之progress标签的应用
progress标签:从名字上来看,估计大家也能猜到这个标签是什么标签了,没错,他是一个进度条.在HTML5中我们终于可以不用模拟了. <progress id="W3Cfuns_pr ...
- sql2008来远程访问sql2005数据库服务器
今天搞了一个下午终于搞定了数据库的远程访问.其基本步骤如下: sql2008的配置: sql server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上 ...