【问题标题】:what are get and post on express js什么是在 express js 上获取和发布
【发布时间】:2020-04-05 17:52:03
【问题描述】:

我无法理解 expressjs 中的 post 和 get 意味着什么。 我正在更改获取并在下面的代码上发布,但没有任何改变。谁能解释一下?

    const express = require('express');
    const router = express.Router();

    router.get('/add', (req, res) => {
    res.send(`
    <html>
        <head>
        <title>Homepage</title>
        </head>
        <body>
        <form method="POST" action="/added">
        <input name="product" type="text">
        <button type="submit">Save</button>
        </form>
        </body>
        </html>
    `)
})

router.post('/added', (req, res) => {
    console.log(req.body);
    res.redirect('/add');
});


module.exports = router;

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    1) Get 用于发出获取请求。 (传递数据的唯一方法是在 URL 中作为查询字符串或参数) - 通常用于获取资源

    2) Post 用于发出 post 请求。 (您也可以在其中传递数据(在正文中)(具有各种格式)。因此它不会出现在 url 中)。 - 通常用于创建资源

    【讨论】:

    • 快乐是我的全部。 :)
    【解决方案2】:

    它们是http请求方法。如果你写了“get”,那么你就无法捕捉到例如一个 post 请求。

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2020-03-26
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2019-12-25
      • 2018-01-02
      • 2018-03-06
      • 2013-02-26
      相关资源
      最近更新 更多