简体   繁体   English

我想从html页面中获取数据并将其作为json数据存储在一个文件中,在node.js中

[英]i want to take the data from html page and store in a file as json data in node.js

The following is my html code i want to read the file ans store the data in a file using node.js 以下是我想读取文件的html代码,并使用node.js将数据存储在文件中

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>The Page Returned by Making Http Call to Node.js</title>
    <style type="text/css">
        table, td {
          border:double;
        }
    </style>
</head>
<body>
    <h1>Product Information Page</h1>
    <table>
        <tr>

            <td>Product Id:</td>
            <td>
                <input type="text" />
            </td>
        </tr>
        <tr>
            <td>Product Name:</td>
            <td>
                <input type="text" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button"  id = "textFieldId" value="Save"/>
            </td>
        </tr>
    </table>


</body>
</html>

The following is my Node.js code i want to write the user input text fields to my filesystem 以下是我想将用户输入文本字段写入我的文件系统的Node.js代码

app.get('/list_user', function (req, res) {
  res.sendFile(path.join((__dirname + '/AppPages/MyPage.html')))
  let inputContent = res.body;
  console.log(inputContent);
  JSON.stringify(inputContent);
  fs.writeFile(__dirname+'/AppPages/data.json',inputContent,function(err){
    if (err) throw err;
    console.log('saved')
  })

I am getting in data.json as undefined 我进入data.json作为未定义

在代码中没有初始化数据对象的位置,但是看起来可以输入内容

  1. Should have get and post page. 应该有获取和发布页面。
  2. Change table to form and set action to your post page. 更改表格以形成表格并将操作设置为您的帖子页面。
  3. On your post routes you can access the data sending via req.body its an oject you have to parse this as string in order for you to write it to a file, Use JSON.stringyfy method, Take note you have to install body-parser and use it on your express. 在发布路线上,您可以访问通过req.body发送的数据,该对象必须解析为字符串才能将其写入文件,请使用JSON.stringyfy方法,请注意,您必须安装body-parser并在您的快递上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM