简体   繁体   English

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

[英]How to save a file on server running Node.js?

I have a server running Node.js. 我有一台运行Node.js的服务器。 I have to read form data from html page ("index.html") and write that data to a txt file. 我必须从html页面(“ index.html”)读取表单数据,并将该数据写入txt文件。

This is my index.html: 这是我的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>

And here is my app.js: 这是我的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');
});

And when I run this, it throws me an error: 当我运行它时,它引发了一个错误:

TypeError: Cannot read property 'ogrNo' of undefined at c:\\Users\\Volkan\\IdeaProjects\\File Upload\\app.js:13:27 at Layer.handle [as handle_request] (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\layer.js:95:5) at next (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\route.js:131:13) at Route.dispatch (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\route.js:112:3) at Layer.handle [as handle_request] (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\layer.js:95:5) at c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\index.js:277:22 at Function.process_params (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\index.js:330:12) at next (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\index.js:271:10) at serveStatic (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\serve-static\\index.js:75:16) at Layer.handle [as handl TypeError:无法在Layer.handle读取c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ app.js:13:27处未定义的属性'ogrNo'[作为handle_request](c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ route处的\\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)(c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ node_modules \\ express \\ lib \\ router \\ route.js:131:13) .dispatch(c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ node_modules \\ express \\ lib \\ router \\ route.js:112:3)[以handle_request](c:\\ Users \\ Volkan \\ IdeaProjects \\ File在c:\\ Users \\ Volkan \\ IdeaProjects \\ File上载\\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)在Function.process_params处的上载\\ node_modules \\ express \\ lib \\ router \\ index.js:277:22 (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 \\在Layer.handle处的serveStatic(c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ node_modules \\ serve-static \\ index.js:75:16)中的router \\ index.js:271:10) e_request] (c:\\Users\\Volkan\\IdeaProjects\\File Upload\\node_modules\\express\\lib\\router\\layer.js:95:5) e_request](c:\\ Users \\ Volkan \\ IdeaProjects \\ File Upload \\ node_modules \\ express \\ lib \\ router \\ layer.js:95:5)

So, what is the cause of my problem or how I can handle this job? 那么,造成我问题的原因是什么?如何处理此工作?

You have to parse incoming data, for that purpose use body-parser 您必须解析传入的数据,为此,请使用body解析器

First install it 首先安装

npm i -S body-parser

Then import it to your code 然后将其导入您的代码

var bodyParser = require('body-parser')

Add parsers 添加解析器

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

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

That's all. 就这样。

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

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