简体   繁体   中英

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

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 handl 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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