如何在运行Node.js的服务器上保存文件?

时间:2017-03-01 08:12:30

标签: javascript node.js express fs

我有一台运行Node.js的服务器。我必须从html页面读取表单数据(" index.html")并将该数据写入txt文件。

这是我的index.html:

<form action="/uploads" method="post">
   <table>
      <tr>
         <td><img src="images/ktulogo.png" width="100" height="100"></td>
         <td>
            <h3>Karadeniz Teknik Üniversitesi Bilgisayar Mühendisliği Bölümü</h3>
         </td>
      </tr>
      <tr>
         <td><br></td>
         <td>
            <hr>
         </td>
      </tr>
      <tr>
         <td>
            <h4>Adınız Soyadınız :</h4>
         </td>
         <td><input type="text" style="margin-left: 5%; width: 40%"></td>
      </tr>
      <tr>
         <td>
            <h4>Öğrenci Numaranız :</h4>
         </td>
         <td><input name="ogrNo" type="text" style="margin-left: 5%; width: 40%"></td>
      </tr>
      <tr>
         <td>
            <h4>Cevabınız :</h4>
         </td>
         <td><textarea name="answer" datatype="text" rows="10" cols="100" style="margin-left: 5%; resize: none"></textarea></td>
      </tr>
   </table>
   </br>
   <center>
      <button id="saveFile" class="btn btn-success" style="width: 30%">Sisteme Yükle</button>
   </center>
</form>

这是我的app.js:

var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs');

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res){
    res.sendFile(path.join(__dirname, 'views/index.html'));
});

app.post('/uploads', function (req, res) {
    var file_name = req.body.ogrNo;
    var file_content = req.body.answer;
    file_content = file_content.replace(/\n/g, "\r\n");

    var stream = fs.createWriteStream(file_name+".txt");
    stream.once('open', function () {
        stream.write(file_content);
        stream.end();
    });
});

var server = app.listen(3000, function(){
    console.log('Server listening on port 3000');
});

当我运行它时,它会抛出一个错误:

  

TypeError:无法读取属性&#39; ogrNo&#39;未定义的      在c:\ Users \ Volkan \ IdeaProjects \ File Upload \ app.js:13:27      在Layer.handle [as handle_request](c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ layer.js:95:5)      在下一个(c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ route.js:131:13)      在Route.dispatch(c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ route.js:112:3)      在Layer.handle [as handle_request](c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ layer.js:95:5)      在c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ index.js:277:22      在Function.process_params(c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ index.js:330:12)      在下一个(c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ index.js:271:10)      在serveStatic(c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ serve-static \ index.js:75:16)      在Layer.handle [as handle_request](c:\ Users \ Volkan \ IdeaProjects \ File Upload \ node_modules \ express \ lib \ router \ layer.js:95:5)

那么,问题的原因是什么或我如何处理这项工作?

1 个答案:

答案 0 :(得分:2)

您必须解析传入的数据,为此目的使用body-parser

首先安装

npm i -S body-parser

然后将其导入您的代码

var bodyParser = require('body-parser')

添加解析器

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// parse application/json
app.use(bodyParser.json());

这就是全部。