【问题标题】:Handle POST request data with Node js使用 Node js 处理 POST 请求数据
【发布时间】:2018-12-12 14:43:58
【问题描述】:

是否有任何方法可以在表单中使用 POST 方法获取段落包含的文本? 我想得到下面段落中的文字:

<form id="selected_activity_form" method="POST" action=<%=token%>>
        <p>text_goes_here</p>
        <input type="submit" value="click" name="btn" id="btn"/>
</form>

附: 我正在使用 Node Js 来处理请求。

【问题讨论】:

  • 你的服务器代码是什么样的?

标签: html node.js forms post


【解决方案1】:

**************************************************** *示例 - 1******************************************

<form method="POST" action="/login">
   <input name="email" placeholder="Email Address" />
   <input name="password" placeholder="Password" />
</form>

const http = require('http'),
const qs = require('querystring');

var server = http.createServer(function(req, res) {
  if (req.method === 'POST' && req.url === '/login') {
    var body = '';
    req.on('data', function(chunk) {
      body += chunk;
    });
    req.on('end', function() {
      var data = qs.parse(body);

      //**** now you can access `data.email` and `data.password`********//

      res.writeHead(200);
      res.end(JSON.stringify(data));
    });
  } else {
    res.writeHead(404);
    res.end();
  }
});

server.listen(80);

**********************************示例 - 2**************** ********

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS3 Contact Form</title>
</head>
<body>
<div id="contact">
    <h1>Send an email</h1>
    <form action="/myaction" method="post">
        <fieldset>
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" placeholder="Enter your full name" />

            <label for="email">Email:</label>
            <input type="email" id="email" placeholder="Enter your email address" />

            <label for="message">Message:</label>
            <textarea id="message" placeholder="What's on your mind?"></textarea>

            <input type="submit" value="Send message" />

        </fieldset>
    </form>
</div>
</body>
</html>

使用 http.createServer 是非常低级的,对于按原样创建 Web 应用程序确实没有用处。

Express 是一个很好的框架,我强烈建议使用它。您可以使用 npm install express 安装它。

如果你有,你可以创建一个基本的应用程序来处理你的表单:

var express = require('express');
    var bodyParser = require('body-parser');
    var app     = express();

    //Note that in version 4 of express, express.bodyParser() was
    //deprecated in favor of a separate 'body-parser' module.
    app.use(bodyParser.urlencoded({ extended: true })); 

    //app.use(express.bodyParser());

    app.post('/myaction', function(req, res) {
      res.send('You sent the name "' + req.body.name + '".');
    });

    app.listen(8080, function() {
      console.log('Server running at http://127.0.0.1:8080/');
    });

您可以使用以下方法使您的表单指向它:

<form action="http://127.0.0.1:8080/myaction" method="post">

您无法在端口 80 上运行 Node 的原因是该端口上已经有一个进程正在运行(它正在为您的 index.html 提供服务)。您还可以使用 Express 来提供静态内容,例如 index.html,使用 express.static 中间件。

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多