Express.js res上的常用方法

本文介绍在使用express框架时,res对象上11种常用方法的使用结合代码分别做了介绍。

1. res.send([body])

  • 功能 : 发送各种类型的数据响应,如BufferStringObjectArray。Express会根据响应类型自动分配正确的内容类型。

  • 示例 :

    javascript 复制代码
    app.get('/', (req, res) => {
      res.send('<h1>你好,世界</h1>'); // 发送text/html响应
    });

2. res.json([body])

  • 功能 : 发送JSON响应。这个方法类似于res.send(),但始终将非Buffer对象转换为JSON字符串后再发送。

  • 示例 :

    javascript 复制代码
    app.get('/user', (req, res) => {
      res.json({ name: '爱丽丝', age: 25 }); // 自动设置内容类型为application/json
    });

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

  • 功能: 结束响应过程。当不需要向客户端发送更多数据时使用此方法。

  • 示例 :

    javascript 复制代码
    app.get('/stream', (req, res) => {
      res.write('数据的第一部分 ');
      res.end('数据的最后部分'); // 结束响应,可以发送或不发送数据
    });

4. res.status(code)

  • 功能: 设置响应的HTTP状态码。

  • 示例 :

    javascript 复制代码
    app.get('/not-found', (req, res) => {
      res.status(404).send('未找到'); // 可与其他res方法链式调用
    });

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

  • 功能: 传输给定路径的文件。根据文件扩展名设置响应的Content-Type HTTP头字段。

  • 示例 :

    javascript 复制代码
    app.get('/download', (req, res) => {
      const filePath = __dirname + '/files/document.pdf';
      res.sendFile(filePath); // 自动设置内容处置为附件
    });

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

  • 功能: 渲染视图并将渲染的HTML字符串发送到客户端。通常与模板引擎一起使用。

  • 示例 :

    javascript 复制代码
    app.get('/dashboard', (req, res) => {
      res.render('dashboard', { user: '爱丽丝' }); // 渲染带有用户数据的'dashboard'视图
    });

7. res.redirect([status,] path)

  • 功能: 重定向到由指定路径派生的URL,可选状态码。

  • 示例 :

    javascript 复制代码
    app.get('/old-page', (req, res) => {
      res.redirect(301, '/new-page'); // 301永久移动状态码
    });

8. res.set(field [, value])

  • 功能 : 将响应的HTTP头 field 设置为 value。也可以传递一个对象以设置多个字段。

  • 示例 :

    javascript 复制代码
    app.get('/with-headers', (req, res) => {
      res.set({
        'Content-Type': 'application/json',
        'Custom-Header': 'HeaderValue'
      });
      res.send({ message: '头部设置' });
    });

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

  • 功能 : 设置名为 name 的cookie的值,附带一个选项对象。

  • 示例 :

    javascript 复制代码
    app.get('/set-cookie', (req, res) => {
      res.cookie('session_id', '12345', { maxAge: 900000, httpOnly: true });
      res.send('Cookie已设置');
    });

10. res.clearCookie(name [, options])

  • 功能: 清除指定名字的cookie。

  • 示例 :

    javascript 复制代码
    app.get('/clear-cookie', (req, res) => {
      res.clearCookie('session_id');
      res.send('Cookie已清除');
    });

11. res.type(type)

  • 功能 : 将Content-Type HTTP头设置为由type确定的MIME类型。

  • 示例 :

    javascript 复制代码
    app.get('/text', (req, res) => {
      res.type('txt').send('文本响应'); // 设置内容类型为text/plain
    });

English version: Expanded and Detailed Explanation of Express.js res Methods

1. res.send([body])

  • Functionality : Sends a response with various types of data, such as a Buffer, String, an Object, or an Array. Express automatically assigns the correct content-type based on the type of the response.

  • Example :

    javascript 复制代码
    app.get('/', (req, res) => {
      res.send('<h1>Hello World</h1>'); // Sends a text/html response
    });

2. res.json([body])

  • Functionality : Sends a JSON response. This method is similar to res.send() but always converts non-Buffer objects into JSON strings before sending them.

  • Example :

    javascript 复制代码
    app.get('/user', (req, res) => {
      res.json({ name: 'Alice', age: 25 }); // Automatically sets Content-Type to application/json
    });

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

  • Functionality: Ends the response process. This method is used when no more data needs to be sent to the client.

  • Example :

    javascript 复制代码
    app.get('/stream', (req, res) => {
      res.write('Part 1 of the data ');
      res.end('Final part of the data'); // Ends the response with or without sending data
    });

4. res.status(code)

  • Functionality: Sets the HTTP status code of the response.

  • Example :

    javascript 复制代码
    app.get('/not-found', (req, res) => {
      res.status(404).send('404 Not Found'); // Chainable with other res methods
    });

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

  • Functionality: Transfers the file at the given path. Sets the Content-Type response HTTP header field based on the file's extension.

  • Example :

    javascript 复制代码
    app.get('/download', (req, res) => {
      const filePath = __dirname + '/files/document.pdf';
      res.sendFile(filePath); // Automatically sets Content-Disposition for attachment
    });

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

  • Functionality: Renders a view and sends the rendered HTML string to the client. Typically used with template engines.

  • Example :

    javascript 复制代码
    app.get('/dashboard', (req, res) => {
      res.render('dashboard', { user: 'Alice' }); // Renders 'dashboard' view with the user data
    });

7. res.redirect([status,] path)

  • Functionality: Redirects to the URL derived from the specified path, with optional status code.

  • Example :

    javascript 复制代码
    app.get('/old-page', (req, res) => {
      res.redirect(301, '/new-page'); // 301 Moved Permanently status code
    });

8. res.set(field [, value])

  • Functionality : Sets the response's HTTP header field to value. Can also pass an object to set multiple fields.

  • Example :

    javascript 复制代码
    app.get('/with-headers', (req, res) => {
      res.set({
        'Content-Type': 'application/json',
        'Custom-Header': 'HeaderValue'
      });
      res.send({ message: 'Headers set' });
    });

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

  • Functionality : Sets cookie name to value, with an options object.

  • Example :

    javascript 复制代码
    app.get('/set-cookie', (req, res) => {
      res.cookie('session_id', '12345', { maxAge: 900000, httpOnly: true });
      res.send('Cookie set');
    });

10. res.clearCookie(name [, options])

  • Functionality : Clears the cookie specified by name.

  • Example :

    javascript 复制代码
    app.get('/clear-cookie', (req, res) => {
      res.clearCookie('session_id');
      res.send('Cookie cleared');
    });

11. res.type(type)

  • Functionality : Sets the Content-Type HTTP header to the MIME type as determined by type.

  • Example :

    javascript 复制代码
    app.get('/text', (req, res) => {
      res.type('txt').send('Text response'); // Sets Content-Type to text/plain
    });
    ``
相关推荐
前端 贾公子2 天前
Express内置的中间件(express.json和express.urlencoded)格式的请求体数据
中间件·json·express
泯泷2 天前
老手机翻新!Express. js v5.0中的新功能
前端·后端·express
读心悦2 天前
express,生成用户登录后的 token
express
悦涵仙子4 天前
创建Express后端项目
前端·javascript·express
Ylucius6 天前
常见服务器大全----都是什么?又有何作用?区别联系是什么?---web,应用,数据库,文件,消息队列服务器,Tomat,Nginx,vite.....
java·前端·javascript·chrome·学习·node.js·express
计算机程序设计开发7 天前
基于Node.js+Express+MySQL+VUE实现的在线电影视频点播网站管理系统的设计与实现部署安装
vue.js·node.js·课程设计·express·计算机毕设·计算机毕业设计
计算机程序设计开发10 天前
基于Node.js+Express+MySQL+VUE新闻网站管理系统的设计与实现
数据库·mysql·node.js·课程设计·express·计算机毕设·计算机毕业设计
读心悦10 天前
express.js 链接数据库
javascript·数据库·express
JOJO___15 天前
Node.js Express中使用joi进行表单验证
node.js·express