簡體   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

以下是我想讀取文件的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>

以下是我想將用戶輸入文本字段寫入我的文件系統的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')
  })

我進入data.json作為未定義

在代碼中沒有初始化數據對象的位置,但是看起來可以輸入內容

  1. 應該有獲取和發布頁面。
  2. 更改表格以形成表格並將操作設置為您的帖子頁面。
  3. 在發布路線上,您可以訪問通過req.body發送的數據,該對象必須解析為字符串才能將其寫入文件,請使用JSON.stringyfy方法,請注意,您必須安裝body-parser並在您的快遞上使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM