Node.js 中的 Response Object函数

首页 / Node.js入门教程 / Node.js 中的 Response Object函数

res 对象表示Express应用程序在收到HTTP请求时发送的HTTP响应。

响应对象属性

以下是与响应对象关联的一些属性的列表。

Sr.No. Properties & 描述
1

res.app

此属性保存对使用中间件的快速应用程序的引用。

2

res.headers已发送

布尔值属性,指示应用程序是否为响应发送了HTTP标头。

3

res.locals

包含响应范围为请求的局部变量的对象

响应对象方法

res.append(field[,value])

res.append(field[,value])

此方法将指定的值附加到HTTP响应标头字段。以下是一些示例-

res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
res.append('Warning', '199 Miscellaneous warning');

res.attachment([filename])

res.attachment([filename])

此方法用于在HTTP响应中将文件作为附件发送。以下是一些示例-

res.attachment('path/to/logo.png');

res.cookie(name,value[,options])

res.cookie(name,value[,options])

此方法用于将cookie名称设置为value。 value参数可以是字符串或转换为JSON的对象。以下是一些示例-

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

res.cookie('cart', { items: [1,2,3] });
res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });

res.clearCookie(name[,options])

res.clearCookie(name[,options])

此方法用于清除按名称指定的cookie。以下是一些示例-

链接:https://www.learnfk.comhttps://www.learnfk.com/nodejs/nodejs-response-object.html

来源:LearnFk无涯教程网

res.cookie('name', 'tobi', { path: '/admin' });
res.clearCookie('name', { path: '/admin' });

res.download(path[,filename] [,fn])

res.download(path[,filename] [,fn])

此方法用于在路径处作为"附件"传输文件,通常,浏览器会提示用户下载。以下是一些示例-

res.download('/report-12345.pdf');

res.download('/report-12345.pdf', 'report.pdf');

res.download('/report-12345.pdf', 'report.pdf', function(err){

});

res.end([data] [,encoding])

res.end([data] [,encoding])

此方法用于结束响应过程。以下是一些示例-

res.end();

res.status(404).end();

res.format(object)

res.format(object)

如果存在,此方法用于对请求对象上的Accept HTTP标头执行内容协商。以下是一些示例-

res.format ({
   'text/plain': function() {
      res.send('hey');
   },

   'text/html': function() {
      res.send('hey'); 
   },

   'application/json': function() {
      res.send({ message: 'hey' });
   },

   'default': function() {
      //log the request and respond with 406
      res.status(406).send('Not Acceptable');
   }
});

res.get(field)

res.get(field)

此方法用于返回由field指定的HTTP响应标头。这是一个示例-

res.get('Content-Type');

res.json([body])

res.json([body])

此方法用于发送JSON响应。以下是一些示例-

res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })

res.jsonp([body])

res.jsonp([body])

此方法用于发送支持JSONP的JSON响应。以下是一些示例-

res.jsonp(null)
res.jsonp({ user: 'tobi' })
res.status(500).jsonp({ error: 'message' })

res.links(links)

res.links(links)

此方法用于连接作为参数属性提供的链接,以填充响应的"链接HTTP"标头字段。以下是一些示例-

res.links ({
   next: 'http://api.example.com/users?page=2',
   last: 'http://api.example.com/users?page=5'
});

res.location(path)

res.location(path)

此方法用于根据指定的path参数设置响应Location HTTP标头字段。以下是一些示例-

res.location('/foo/bar');
res.location('foo/bar');
res.location('http://example.com');

res.redirect([status,]path)

res.redirect([status,]path)

此方法用于重定向到具有特定HTTP状态代码状态的从指定路径派生的URL。以下是一些示例-

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');

res.render(view [,locals] [,callback])

res.render(view [,locals] [,callback])

此方法用于呈现视图,并将呈现的HTML字符串发送给客户端。以下是一些示例-

//send the rendered view to the client
res.render('index');

//pass a local variable to the view
res.render('user', { name: 'Tobi' }, function(err, html) {
   //...
});

res.send([body])

res.send([body])

此方法用于发送HTTP响应。以下是一些示例-

res.send(new Buffer('whoop'));
res.send({ some: 'json' });
res.send('<p>some html</p>');

res.sendFile(path[,options] [,fn])

res.sendFile(path[,options] [,fn])

此方法用于在给定路径下传输文件。根据文件名的扩展名设置Content-Type响应HTTP标头字段。这是一个示例-

res.sendFile(fileName, options, function (err) {
   //...
});

res.sendStatus(statusCode)

res.sendStatus(statusCode)

此方法用于将响应HTTP状态代码设置为statusCode并将其字符串表示形式作为响应主体发送。以下是一些示例-

res.sendStatus(200); //equivalent to res.status(200).send('OK')
res.sendStatus(403); //equivalent to res.status(403).send('Forbidden')
res.sendStatus(404); //equivalent to res.status(404).send('Not Found')
res.sendStatus(500); //equivalent to res.status(500).send('Internal Server Error')

res.set(field[,value])

res.set(field[,value])

此方法用于将响应的HTTP标头字段设置为value。以下是一些示例-

res.set('Content-Type', 'text/plain');

res.set ({
   'Content-Type': 'text/plain',
   'Content-Length': '123',
   'ETag': '12345'
})

res.status(code)

res.status(code)

此方法用于设置响应的HTTP状态。以下是一些示例-

res.status(403).end();
res.status(400).send('Bad Request');
res.status(404).sendFile('/absolute/path/to/404.png');

res.type(type)

res.type(type)

此方法用于将Content-Type HTTP标头设置为MIME类型。以下是一些示例-

res.type('.html');              //=> 'text/html'
res.type('html');               //=> 'text/html'
res.type('json');               //=> 'application/json'
res.type('application/json');   //=> 'application/json'
res.type('png');                //=> image/png:

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

手把手教你玩音乐 -〔邓柯〕

基于人因的用户体验设计课 -〔刘石〕

容量保障核心技术与实战 -〔吴骏龙〕

郭东白的架构课 -〔郭东白〕

自动化测试高手课 -〔柳胜〕

程序员职业规划手册 -〔雪梅〕

云原生基础架构实战课 -〔潘野〕

好记忆不如烂笔头。留下您的足迹吧 :)