Express.js 如何接收post参数

Express.js 如何接收post参数

Express是一个小型框架,坐落在Node.js的Web服务器功能之上,简化了其API并添加了有用的新功能。通过中间件和路由,它可以更容易地组织应用程序的功能;它为Node.js的HTTP对象添加了一些有用的工具;它便于渲染动态HTTP对象。Express.js提供了各种处理不同类型的传入请求(如get、post等)的方法。在本文中,我们将讨论如何在express.js中接收post参数。

POST参数 可以使用 express.urlencoded() 中间件和 req.body 对象从表单接收。express.urlencoded()中间件有助于解析来自客户端的数据。

语法:

express.urlencoded( [options] )

参数: options参数包含了各种属性,如extended、inflate、limit、verify等等。

返回值: 它返回一个对象。

示例: 让我们逐步讨论如何在express.js中接收POST参数。

步骤1: 创建一个“ app.js ”文件,并使用npm初始化您的项目。

npm init

步骤2: 创建一个名为“ index.html ”的文件,并使用npm安装 express 包。

npm install express

项目结构:

Express.js 如何接收post参数

步骤3: 现在让我们首先编写“ index.html ”文件。在该文件中,我们创建一个表单,将该表单以 method 为“ POST ”提交到稍后在“ app.js ”文件中声明的“ /submit ”路由上。

index.html

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta http-equiv="X-UA-Compatible" 
        content="IE=edge"> 
    <meta name="viewport" content= 
        "width=device-width, initial-scale=1.0"> 
</head> 
  
<body> 
    <!-- Action to the same route as in app.post() method -->
    <form action="/submit" method="post"> 
        Name: <input type="text" name="name"> 
        <br><br> 
  
        Email: <input type="email" name="email"> 
        <br><br> 
  
        Gender: <br> <input type="radio" name="gender" id="male"> 
        Male 
        <br> 
  
        <input type="radio" name="gender" id="female"> 
        Female 
        <br> 
  
        <input type="radio" name="gender" id="private"> 
        Don't want to disclose 
        <br><br> 
  
        Hobbies: <br> <input type="checkbox" 
            name="painting" id="painting"> 
        Painting 
        <br> 
  
        <input type="checkbox" name="dancing" id="dancing"> 
        Dancing 
        <br> 
          
        <input type="checkbox" name="singing" id="singing"> 
        Singing 
        <br> <br> 
          
        <input type="file" name="file" id="">  
        <br> <br> 
          
        <button type="submit">Submit</button> 
    </form> 
</body> 
  
</html> 

步骤4:

  • 现在在 app.js 文件中,我们将定义根目录的 GET 请求。对于 GET 请求,我们将“ index.html ”文件发送给客户端。
  • 对于“‘ / submit ’” POST 请求,我们从表单中获取输入,并将其打印到控制台上 req.body 。req.body是一个包含表单所有属性的对象。我们可以使用 **req.body. ** 访问任何属性。例如:在这种情况下,使用req.body.name。

app.js

// Requiring express npm package 
const express = require('express') 
  
const app = express() 
  
// Using express.urlencoded middleware 
app.use(express.urlencoded({ 
    extended: true
})) 
  
// Handling Post request 
app.post('/submit',function(req,res){ 
  
    // Can access all parameters from req.body 
    console.log('POST parameter received are: ',req.body) 
  
    res.redirect('/') 
}) 
  
// Get request  
app.get('/',function(req,res){ 
      
    // Sent index.html file to the client 
    res.sendFile(__dirname+'/index.html') 
}) 
  
// Creating server at port 3000 
app.listen(3000,function(req,res){ 
    console.log('started listening at server 3000') 
})

步骤5: 使用以下命令运行 app.js 文件:

node app.js

步骤6 。在任何浏览器中输入本地服务器地址。

http://localhost:3000/

输出: 因此,通过这种方式我们可以在express.js中接收POST参数。

Express.js 如何接收post参数

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程