• Node.js Video Tutorials

Node.js - 響應物件



res 物件表示 Express 應用程式在接收到 HTTP 請求時傳送的 HTTP 響應。

響應物件屬性

以下是與響應物件關聯的一些屬性列表。

序號 屬性及描述
1

res.app

此屬性儲存對正在使用中介軟體的 Express 應用程式例項的引用。

2

res.headersSent

布林屬性,指示應用程式是否已為響應傳送 HTTP 標頭。

3

res.locals

一個物件,包含作用域限定到請求的響應本地變數

響應物件方法

res.append(field [, value])

res.append(field [, value])

此方法將指定的值追加到 HTTP 響應標頭欄位。以下是一些示例 -

res.append('Link', ['<https:///>', '<https://: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])

此方法用於清除由名稱指定的 cookie。以下是一些示例 -

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

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

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

此方法用於將 path 位置的檔案作為“附件”傳輸。通常,瀏覽器會提示使用者下載。以下是一些示例 -

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)

此方法用於連線作為引數屬性提供的連結,以填充響應的 Link 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)

此方法用於重定向到從指定的 path 派生的 URL,並使用指定的 HTTP 狀態程式碼 status。以下是一些示例 -

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:
nodejs_express_framework.htm
廣告