关于express模块下request.on方法的详细说明

关于express模块下request.on方法的详细说明

一般用于post请求,获取参数
request.on()方法是用于监听 HTTP 请求事件
request.on('data', fn)用来获取消息体
request.on('end', fn) 用来拼接消息体
app.post("/info",(request,response)=>{
     let body = '';
     request.on("data",thunk=>{
         body += thunk;
     })
     request.on("end",()=>{
		/*
		*querystring.parse(str, 分隔符, 赋值符, 
		*可接受字符串的最大长度,默认为1000),将字符串转成对象
		*/
         console.log(querystring.parse(body));
         response.json({
             ok: 1,
             msg: "post->info"
         })
     })
 })

上述代码主要是通过post方式发送请求,使用request.on方法获取到消息体数据,并将消息体数据拼接至字符串中,最后将字符串通过querystring方法转换为对象,并输出。
简单来说,通过post方式,得到是消息体数据,从而操作消息体数据,而get方式,获取的是路由中的参数数据,可操作的则是路由中的参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
const express = require('express'); const app = express(); const sql = require('mssql'); const BrowserWindow = electron.BrowserWindow; // 创建窗口引用 const mainWindow = null; // 声明要打开的主窗口 app.on('ready', () => { mainWindow = new BrowserWindow({ width: 300, // 设置打开的窗口大小 height: 300, // 增加全量使用node.js,因为我们要使用node里的fs模块 // enableRemoteModule 设置为true,否则不允许在渲染器上使用 remote webPreferences: { nodeIntegration: true, contextIsolation: false, enableRemoteModule: true } }); mainWindow.loadFile('register.html'); // 加载哪个页面 mainWindow.on('closed', () => { // 监听关闭事件,把主窗口设置为null,否则内存会占用越来越多 mainWindow = null; }) }) // SQL Server 数据库配置 const config = { server: '192.168.10.223', user: 'sa', password: 'Servo01', database: 'demo', options: { encrypt: true // 如果需要 SSL 连接,设置为 true } }; app.use(express.json()); app.post('/register', function (req, res) { const { name, email, password } = req.body; // 连接 SQL Server 数据库 sql.connect(config, function (err) { if (err) { console.log(err); res.status(500).send('数据库连接失败'); return; } // 执行插入操作 const query = 'INSERT INTO username (name, email, password) VALUES (@name, @email, @password)'; const request = new sql.Request(); request.input('name', sql.NVarChar, name); request.input('email', sql.NVarChar, email); request.input('password', sql.NVarChar, password); request.query(query, function (err, result) { if (err) { console.log(err); res.status(500).send('注册失败'); return; } res.send('注册成功'); }); }); }); module.export = //开启服务器监听 app.listen(4000, () => { console.log('#localhost:4000'); })以上代码中导出怎么写
07-15

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

engineer_why

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值