javascript - How to save a file on server running Node.js? - Stack Overflow

I have a server running Node.js. I have to read form data from html page ("index.html") and w

I have a server running Node.js. I have to read form data from html page ("index.html") and write that data to a txt file.

This is my 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:

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 handle_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?

I have a server running Node.js. I have to read form data from html page ("index.html") and write that data to a txt file.

This is my 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:

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 handle_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?

Share Improve this question edited Mar 1, 2017 at 9:07 Morteza Asadi 1,8552 gold badges22 silver badges40 bronze badges asked Mar 1, 2017 at 8:12 vakkayavakkaya 311 gold badge1 silver badge6 bronze badges 3
  • check your request ! – Sikorski Commented Mar 1, 2017 at 8:15
  • 1 body is undefined. You'r missing github./expressjs/body-parser – Boris Charpentier Commented Mar 1, 2017 at 8:16
  • I handle it ! You're right @BorisCharpentier, thanks ! – vakkaya Commented Mar 1, 2017 at 8:24
Add a ment  | 

1 Answer 1

Reset to default 2

You have to parse ining data, for that purpose use body-parser

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745466112a4628924.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信