目录

响应对象(Response Object)

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

响应对象属性

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

Sr.No. 属性和描述
1

res.app

此属性包含对使用中间件的快速应用程序实例的引用。

2

res.headersSent

布尔属性,指示应用程序是否为响应发送了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参数可以是转换为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])

此方法用于清除name指定的cookie。 以下是一些例子 -

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)

此方法用于返回由字段指定的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)

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

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

res.redirect([status,] path)

res.redirect([status,] path)

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

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:
↑回到顶部↑
WIKI教程 @2018