Express.js 响应对象基础教程文档
收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體
Express.js响应对象
响应对象(res)指定Express应用在收到HTTP请求时发送的HTTP响应。
它做什么
它将响应发送回客户端浏览器。 它可帮助您放置新的cookie值,并将其写入客户端浏览器(在跨域规则下)。 一旦您执行res.send()或res.redirect()或res.render(),便无法再次执行此操作,否则将出现未捕获的错误。响应对象属性
让我们看一下响应对象的一些属性。
属性 | 说明 |
res.app | 它包含对使用中间件的express应用程序实例的引用。 |
res.headers已发送 | 这是一个布尔型属性,指示应用程序是否为响应发送了HTTP标头。 |
res.locals | 它指定一个对象,该对象包含范围为请求的响应局部变量 |
响应对象方法
以下是一些方法:
响应追加方法
语法:
res.append(field [, value])
此方法将指定的值附加到HTTP响应标头字段。这意味着如果指定的值不合适,则此方法可以解决该问题。
示例:
res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']); res.append('Warning', '199 Miscellaneous warning');
响应附件方法
语法:
res.attachment([filename])
此方法使您可以在HTTP响应中将文件作为附件发送。
示例:
res.attachment('path/to/js_pic.png');
响应Cookie方法
语法:
res.cookie(name, value [, options])
此方法用于将cookie名称设置为value。该值可以是转换为JSON的字符串或对象。
示例:
res.cookie('name', 'Aryan', { domain: '.xyz.com', path: '/admin', secure: true }); res.cookie('Section', { Names: [Aryan,Sushil,Priyanka] }); res.cookie('Cart', { items: [1,2,3] }, { maxAge: 900000 });
响应ClearCookie方法
语法:
res.clearCookie(name [, options])
顾名思义,clearCookie方法用于清除由名称指定的cookie。
示例:
要设置Cookie
res.cookie('name', 'Aryan', { path: '/admin' });
要清除Cookie:
res.clearCookie('name', { path: '/admin' });
响应下载方法
语法:
res.download(path [, filename] [, fn])
此方法将路径中的文件作为"附件"传输,并强制浏览器提示用户下载。
示例:
res.download('/report-12345.pdf');
响应结束方法
语法:
res.end([data] [, encoding])
此方法用于结束响应过程。
示例:
res.end(); res.status(404).end();
响应格式方法
语法:
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'); } });
响应Get方法
语法:
res.get(field)
此方法提供由field指定的HTTP响应标头。
示例:
res.get('Content-Type');
响应JSON方法:
语法:
res.json([body])
此方法以JSON格式返回响应。
示例:
res.json(null) res.json({ name: 'ajeet' })
响应JSONP方法
语法:
res.jsonp([body])
此方法以JSONP和JSONP支持返回响应。
示例:
res.jsonp(null) res.jsonp({ name: 'ajeet' })
响应链接方法
语法:
res.links(links)
此方法通过加入作为参数属性提供的链接来填充响应的"链接HTTP"头字段。
示例:
res.links({ next: 'http://api.rnd.com/users?page=5', last: 'http://api.rnd.com/users?page=10' });
响应定位方法
语法:
res.location(path)
此方法用于根据指定的路径参数设置响应位置HTTP标头字段。
示例:
res.location('http://xyz.com');
响应重定向方法
语法:
res.redirect([status,] path)
此方法重定向到从具有指定HTTP状态的指定路径派生的URL
示例:
res.redirect('http://example.com');
响应呈现方法
语法:
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: 'aryan' }, function(err, html) { // ... });
响应发送方法
语法:
res.send([body])
此方法用于发送HTTP响应。
示例:
res.send(new Buffer('whoop')); res.send({ some: 'json' }); res.send(' .....some html ');
响应sendFile方法
语法:
res.sendFile(path [, options] [, fn])
此方法用于在给定路径下传输文件。它根据文件名的扩展名设置Content-Type响应HTTP标头字段。
示例:
res.sendFile(fileName, options, function (err) {
// ...
});
响应集方法
语法:
res.set(field [, value])
此方法用于将HTTP标头字段的响应设置为value。
示例:
res.set('Content-Type', 'text/plain'); res.set({ 'Content-Type': 'text/plain', 'Content-Length': '123', })
响应状态方法
语法:
res.status(code)
此方法设置响应的HTTP状态。
示例:
res.status(403).end(); res.status(400).send('Bad Request');
响应类型方法
语法:
res.type(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: