2

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?

3
  • check your request ! Commented Mar 1, 2017 at 8:15
  • 1
    body is undefined. You'r missing github.com/expressjs/body-parser Commented Mar 1, 2017 at 8:16
  • I handle it ! You're right @BorisCharpentier, thanks ! Commented Mar 1, 2017 at 8:24

1 Answer 1

2

You have to parse incoming 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe in 2017 it's better to use const than var. Maybe not. Any idea ?
Sure it's better to use const for such things, but it makes no sense for this case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.