目录
app.get(path:String,callback:Function)
app.post(path:String,callback:Function)
一、启动index.js服务器
上集我展示了一个接收访问"/"的例子,但我们还没有启动它,也没有访问它。
我们要直接在电脑上启动index.js也就是说,服务器就是我们的电脑,我们电脑的域名是http:localhost,ip地址是127.0.0.1。
因为这个项目是用node.js创建的,所以我们要用node.js启动index.js,在终端输入以下代码:
node index.js
如果你像上集一样把app.listen的callback写上了
app.listen(port,()=>console.log("sever started"));
等到服务器启动完毕,那你就会在终端上看到"server started"(终端就相当于控制台console)
这时,你在浏览器上输入localhost:3000或127.0.0.1:3000,你就会看见页面上写着Welcome To Express!
(如果你是按我在上集的最后举的例子输入代码的话)
二、具体语法
好,我们话不多说,开始吧!
1.接收请求
app.get(path:String,callback:Function)
path是访问的路径,不用写域名端口等,前面必须有一个"/",比如这样:
app.get("/",(req,res,next)=>res.send("Hello world!"));
那你访问localhost:3000/时,他就会返回Hello world!
callback就是path路径被访问时要执行的函数。
它有3个参数:
(req:Request,res:Response,next:NextFunction)=>{/*some code*/}
Request是请求(客户端)的对象
Response是回应(服务器)的对象
NextFunction是其他能做出响应的路由的函数,是一个Function。
app.post(path:String,callback:Function)
用法和app.get几乎一样,这里用来回应post请求,与get唯一的不同是有req.body,是post请求的参数,后面将继续讲解。
app.use(callback:Function)
无论是什么访问了类别,什么路径都会运行这里的callback,callback的参数和刚才说的一样。
如果你把它放在最前面,那么无论怎样都会运行这个函数。
NextFunction就是后面所有接收请求的函数。
如果你没有调用next();,请求就到此为止。
但如果你调用了,那就会运行符合要求的接收请求的函数。
例如:
const express=require("express");
const app=express();
const port=3000;
app.set("port",port);
app.listen(port,()=>console.log("server started"));
app.use((req,res,next)=>{
res.write("Hello");
});
app.get("/a",(req,res,next)=>{
res.end(" World");
});
app.get("/b",(req,res,next)=>{
res.write(" World");
next();
});
app.use((req,res)=>{
res.end("!");
});
那么,你访问/a会返回"Hello World"
访问/b就会返回"Hello World!"
2.res详解
res.send(message)
回应message。
在一个请求里,这个send函数只能执行一次,否则报错,所以每次执行完最好马上return;
message可以是string,json,blob,arraybuffer,与客户端的new XMLHttpRequest().responseType的值有关。
res.json(message)
回应message,为JSON对象。
res.write(message)
回应message,一般为字符串,在一次请求里可以执行多次。
res.end(message)
回应mssage,并结束请求,此函数执行后,不允许再次回应。
如果只有res.write没有res.end请求讲无法结束。
res.status(status)
返回http状态码(Http Status Code)status
例子
const express=require("express");
const app=express();
const port=3000;
app.set("port",port);
app.listen(port,()=>console.log("server started"));
app.get("/a",(req,res,next)=>{
res.send("Hello World!");
});
app.get("/b",(req,res,next)=>{
res.write("Hello");
res.end(" World!");
});
app.get("/c",(req,res,next)=>{
res.json({msg:"Hello World!"});
});
app.get("/d",(req,res,next)=>{
res.status(404).send("404 Not Found");
});
2.req详解
req.url
访问的链接去掉http://localhost:3000后剩下的部分,开头有/,包括问号后面的参数。
req.path
访问的链接去掉http://localhost:3000和问号后的参数后剩下的部分。
req.hostname
这个很好理解,就是index.js所创建或使用的路由器域名,没有端口。
req.ip
这个也很好理解,同样是index.js所创建或使用的路由器的ip地址。
以上的值都是字符串,和访问路径有关。
req.query
Object,在链接里问号后面的参数的对象形式。
req.body
Any,在POST请求里上传的参数。
req.method
String,请求的类别,可为:GET,POST,PUT,DELETE
req.xhr
Boolean,是否使用XMLHttpRequest访问。
例子
GET http://localhost:3000/hello
req.url="/hello";
req.path="/hello";
req.hostname="localhost";
req.ip="127.0.0.1";
req.query={};
req.body=undefined;
req.method="GET";
req.xhr=false;//在网页上输入链接
req.xhr=true;//使用new XMLHttpRequest()完成的请求
GET http://localhost:3000/demo?index=5
req.url="/demo?index=5";
req.path="/demo";
req.hostname="localhost";
req.ip="127.0.0.1";
req.query={index:5};
req.body=undefined;
req.method="GET";
POST http://localhost:3000/index //参数这里不写
req.url="/index";
req.path="/index";
req.hostname="localhost";
req.ip="127.0.0.1";
req.query={};
req.body;//这里比较复杂,以后的教程会详细说。
req.method="POST";
这篇文章就到此结束了,下集更精彩!
下集:渲染(render)HTML页面https://blog.csdn.net/ttowill/article/details/128886132?spm=1001.2014.3001.5502。