Express是什么 ?

Express是一个简洁,灵活的node.js Web应用开发框架 ,提供一系列的强大功能,可以快速的搭建一个具有完整功能的网站。

 

1、NPM 安装 

npm install express

2、获取、引用 

var express=require("express");
var app=express();

现在我们可以通过变量 "app"就可以调用express的各种方法了。

 

创建应用

认识了Express框架,现在我们做第一个express的应用 

在项目的住文件app.js添加如下内容: 

var express=require("express");
var app=express();

app.get("/",function(request,response){
    response.send("hello world");
});

app.listen(8088);//设置端口号

然后打开cmd定位到app.js的目录,如图:

express 显示中文 express语言_中间件

运行效果如图,浏览器输出“hello world”

express 显示中文 express语言_Code_02

 

get请求

get方法-----根据请求路径来处理客户端发出的GET请求

语法: app.get(path,function(request,response))

path为请求的路径,第二个参数为处理请求的回调函数,request和response分别代表请求信息和响应信息

var express=require("express");
var app=express();

app.get("/",function(request,response){
    response.send("welcome to the homepage");
});

app.get("/about",function(request,response){
    response.send("welcome to the about page");
});

app.get("*",function(request,response){
    response.send("404 error");
});

app.listen(8088);

node启动app.js命令: node app.js

然后再浏览器中输入:

1)http://127.0.0.1:8088/ 输出为:“welcome to the homepage”

2) http://127.0.0.1:8088/about 输出为:“welcome to the about page”

3) http://127.0.0.1:8088/about/s 输出为:“404 error”

 

all函数的基本用法

如果使用all函数定义中间件,那么相当于所有的请求都必须通过该中间件

语法:app.all(path,function(request,response));

例如:

var express=require("express");
var app=express();

app.all("*",function(request,response,next){
    response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});//设置响应头属性值
    console.log("这是中间件哦,所有的请求都要经过此的哦");
    next();
});

app.get("/",function(request,response){
    response.end("欢迎来到首页");
});

app.get("/about",function(request,response){
    response.end("欢迎来到about页面");
});

app.get("*",function(request,response){
    response.end("404 error");
});

app.listen(8088);

运行结果如图:

express 显示中文 express语言_Express_03

 

 

use 基本用法

use是express调用中间件的方法,它返回一个函数

语法:app.use([path],function(request,response,next){});

连续调用两个中间件,如下例子:

express 显示中文 express语言_Code_04

express 显示中文 express语言_中间件_05

var app=express();

app.use(function(request,response,next){
    console.log("method:"+request.method+",url="+request.url);
    next();
});

app.use(function(request,response){
    console.log("调用第二个中间件");
    response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
    response.end("例子,连续调用两个中间件");
});
app.listen(8088);

View Code

运行此js,在浏览器随便输入一个地址:http://127.0.0.1:8088/abouts,浏览器上的内容为:"例子,连续调用两个中间件"

同时控制台输出两行内容为:

method:GET,url=/abouts

调用第二个中间件

备注:回调函数中的next参数,表示接受其他中间件的调用

        函数体中的next()表示将请求数据传递到下一个中间件。

以上例子调用第一个中间件,在控制台输出一行信息,然后通过next(),调用第二个中间件,输出控制台信息以及Http回应。由于第二个中间件没有调用next方法,所以request对象就不再向后传递了。

 

use方法不仅可以调用中间件,还可以根据请求的网址,返回不同的网页内容,例如:

express 显示中文 express语言_Code_04

express 显示中文 express语言_中间件_05

var express=require("express");
var app=express();

app.use(function(request,response,next){
    if(request.url=="/"){
        response.send("Welcome to the homepage");
    }else if(request.url=="/about"){
        response.send("Welcome to the about page");
    }else{
        next();
    }
});

app.use(function(request,response){
    response.send("404 error");
});
app.listen(8088);

View Code

当输入的网址不存在时,会执行中间件,返回 “404 error”

 

获取主机名,路径名,query参数等,例如:

express 显示中文 express语言_Code_04

express 显示中文 express语言_中间件_05

var express=require("express");
var app=express();

app.get("*",function(request,response){
    console.log(request.path);
    response.send("request.host获取主机名:"+request.host+",请求的路径名为:"+request.path+",query参数为:"+request.query.page);
});
app.listen(8088);

View Code

当在浏览器中输入:http://127.0.0.1:8088/about/use?page=1

浏览器中的输出为:request.host获取主机名:127.0.0.1,请求的路径名为:/about/use,query参数为:1

通过req.query获取get请求路径的对象参数值。

格式:req.query.参数名;请求路径如下示例:

例1: /search?n=Lenka

req.query.n // "Lenka"

例2: /shoes?order=desc&shoe[color]=blue&shoe[type]=converse

req.query.order  // "desc"
 
req.query.shoe.color  // "blue"
 
req.query.shoe.type  // "converse"

 

和query属性一样,也可以使用param获取请求的参数,例如:

express 显示中文 express语言_Code_04

express 显示中文 express语言_中间件_05

var express=require("express");
var app=express();

app.get("*",function(request,response){
    response.send("使用req.param属性获取参数对象值:page="+request.param("page"));
});
app.listen(8088);

View Code

在浏览器中输入:http://127.0.0.1:8088/about/use?page=12

输出的内容为:使用req.param属性获取参数对象值:page=12

例2:我们也可以获取具有相应路由规则的请求对象,假设路由规则为 /user/:name/,请求路径/user/mike,如下:

app.get("/user/:name/", function(req, res) {
    console.log(req.param("name")); //mike
    res.send("使用req.param属性获取具有路由规则的参数对象值!");
});

PS:所谓“路由”,就是指为不同的访问路径,指定不同的处理方法

 

params基本用法

格式:request.params.参数名

例如:

express 显示中文 express语言_Code_04

express 显示中文 express语言_中间件_05

var express=require("express");
var app=express();

app.get("/user/:name/",function(request,response){
    console.log(request.params.name);
    response.send("使用req.params属性获取具有路由规则的参数对象值,name="+request.params.name);
})

app.get("/user/:name/:id",function(request,response){
    console.log(request.params.id);
    response.send("使用req.params属性复杂路由规则的参数对象值,name="+request.params.name+",id="+request.params.id);
});
app.listen(8088);

View Code

在浏览器中输入:http://127.0.0.1:8088/user/alice/888

输出的内容为:使用req.params属性复杂路由规则的参数对象值,name=alice,id=888