Express.js 中的 res.links() 方法
res.links() 方法用于连接两个链接,这两个链接是作为参数的属性提供的,以填充响应的 Link Http 标头值。
语法
res.links( links )
实例 1
创建一个名为 "resLinks.js" 的文件,并复制以下代码片段。创建文件后,使用 "node resLinks.js" 命令运行此代码,如下例所示 −
// res.links(links) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
res.links({
next: 'https://tutorialspoint.com?page=1',
middle: 'https://tutorialspoint.com?page=2',
last: 'https://tutorialspoint.com?page=3'
});
console.log(res.get('link'));
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 请求访问以下端点 − localhost:3000/api
输出
C:\home
ode>> node resLinks.js Server listening on PORT 3000 <https://tutorialspoint.com?page=1>; rel="next", <https://tutorialspoint.com?page=2>; rel="middle", <https://tutorialspoint.com?page=3>; rel="last"
实例 2
我们来看一下另一个实例。
// res.links(links) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res, next){
res.links({
next: 'https://tutorialspoint.com?page=1',
middle: 'https://tutorialspoint.com?page=2',
last: 'https://tutorialspoint.com?page=3'
});
// Using middleware
next();
})
app.get('/api', function(req, res){
console.log(res.get('link'));
res.send();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 请求访问以下端点 − localhost:3000/api
输出
C:\home
ode>> node resLinks.js Server listening on PORT 3000 <https://tutorialspoint.com?page=1>; rel="next", <https://tutorialspoint.com?page=2>; rel="middle", <https://tutorialspoint.com?page=3>; rel="last"
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP