express 中使用post方法

14 篇文章 0 订阅

express 中使用post方法

Express中默认都使用body-parser作为请求体解析post数据,这个模块也能解析:JSON、Raw、文本、URL-encoded格式的请求体。
首先在项目目录安装body-parser:

npm install body-parser

在app.js中使用该插件

const express = require('express')
const bodyParser = require('body-parser')
const router = require('./router')//引用模块
const app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// 全局 中间件  解决所有路由的 跨域问题
app.all('*',function (req,res,next) {
    res.header('Access-Control-Allow-Origin','*')
    res.header('Access-Control-Allow-Headers','X-Requested-With,Content-Type')
    res.header('Access-Control-Allow-Methods','GET,POST,OPTIONS')
    next()
})
app.use(router)
app.listen(3000,function () {
    console.log('服务器启动成功')
    console.log('http://127.0.0.1:3000')
})

router目录下index.js文件:

const {Router} = require('express')
const path = require('path')
const router = new Router()

router.get('/',function (req,res){
    res.send({
        msg: 'hi node express'
    })
})

router.post('/login',function (req,res){

    // req: 客户端 携带的信息
    // console.log(req.query)
    // console.log(req.params)
    console.log(req.body)
    // console.log(req.get('Origin'))
    // console.log(req.url)

    // res 服务端 发送给 客户端的信息(数据)
    // res.send({
    //     msg: 'login 接口'
    // })
    // res.end('不会自动转换数据 容易乱码')
    // res.download('./static/dj.jpg') // 相对地址
    // res.sendFile(path.resolve('static','dj.jpg')) // 绝对地址
    // res.redirect('https://www.shiguangkey.com/')
    // res.redirect('https://www.shiguangkey.com/')
    res.set('sadsadsadsafdgsfadgfsdfsd','789')
    res.send({
        msg: 'set演示'
    })
})


module.exports = router

使用postman进行测试接口:

成功!!!

在这里插入图片描述

  • 12
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Express 设置 POST 请求有几个步骤: 1. 首先,确保你已经安装了 Express 框架并在你的项目引入了它。你可以使用以下命令安装 Express: ``` npm install express ``` 2. 在你的应用程序使用 `require` 引入 Express 模块: ```javascript const express = require('express'); const app = express(); ``` 3. 添加一个路由来处理 POST 请求。使用 app.post() 方法来定义一个 POST 请求的路由,并传入对应的路径和回调函数。在回调函数,你可以处理 POST 请求的逻辑: ```javascript app.post('/your-route', (req, res) => { // 处理 POST 请求的逻辑 }); ``` 4. 在回调函数,你可以通过 `req.body` 对象来获取 POST 请求的数据。但是在 Express ,默认情况下,`req.body` 是 undefined,因为 Express 并没有内置的请求体解析器。你需要使用中间件来解析请求体。最常用的中间件是 `body-parser`。 先安装 `body-parser`: ``` npm install body-parser ``` 然后在应用程序引入并使用它: ```javascript const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: true })); ``` 这样,`req.body` 就可以用来获取 POST 请求的数据了。 5. 最后,启动你的 Express 应用程序: ```javascript app.listen(3000, () => { console.log('应用程序已启动在端口 3000'); }); ``` 以上就是在 Express 设置 POST 请求的基本步骤。你可以根据具体的需求进行进一步的处理和逻辑编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值