在Node.js(快速框架)中处理文本的有效方法是什么?

时间:2019-02-04 04:25:51

标签: node.js express io

所以我有一个Web服务器,它将根据用户值发送HTML。 我有一个小的处理程序,它可以读取现有文件(包含密码)并允许用户输入。即有时它会工作有时不会。

每次都能使用的摘录

app.all('/acceptForm',function(req,res){
  if (req.method === 'POST') {
        let body = '';
        var match = 0;
        req.on('data', chunk => {
            body += chunk.toString();
        });
        req.on('end', () => {
            //get the uid to compare later on in the program 
          uid = parse(body).uid_text;
         //read the UID file.
          var lineReader = require('readline').createInterface({
            input: require('fs').createReadStream(__dirname+'/uid.txt')
            ... 
// write the other information to a file which would be later on re -opened again to read the things again
which have the file name of the 'uid'


 firstname =  parse(body).first_name;
               lastname = parse(body).last_name;
                mothername = parse(body).mother_name;
                fathername = parse(body).father_name;
                email = parse(body).email;
                profession = parse(body).profession_text;
                gender = parse(body).gender;
                language = parse(body).lang_0;
                married = parse(body).married;
                birthday =  parse(body).dateofbirth;
             //write the UID and other things to the text file
             console.log(language);
             var fileContent = uid +'|' + firstname +'|'+ lastname +'|' + mothername +'|' + fathername +'|' + email+'|' + profession+'|' + gender+'|' + married+'|' +birthday + '|';
               var filepath = __dirname+"/users/"+uid + ".txt";

               fs.writeFile(filepath, fileContent, (err)


...
            lineReader.on('line', function (line) {
           if(line == uid) {
                    // if the uid is found...
            res.cookie('name',uid, {signed: true}); //write the  uid as a cookie back
            res.sendFile(__dirname+'/CE/ENG/Kids.html');
             }   else{
             //some failure message 
}  
          });

          });
  } 
}

问题是用户将其发送到另一个文件后服务器就失去了与客户端的联系。为了抵消我添加了同一个cookie的系统的麻烦,现在还有安全隐患风险。

正在处理来自kids.html的响应,该响应存储在另一个文件中。 (成功运行的可能性很小)。

app.all('/return',function(req,res){
  if (req.method === 'POST') {


  //read the UID file.
  var lineReader = require('readline').createInterface({
    input: require('fs').createReadStream(__dirname+'/uid.txt')
  });
  //Handling the information from the client.
  lineReader.on('line', function (line) {
   if(line == req.signedCookies['name']) {
     //uid matches with the database 
     fs.readdir( __dirname+"/users/", (err, files) => {
      files.forEach(file => {
       if(file == req.signedCookies['name'] + ".txt"){
        let questiondata = '';
        req.on('data', chunk => {
            questiondata += chunk.toString();

        });


        req.on('end', () => {
          var cleaneddata = questiondata.split('%2C'); //%2C is a spliting term {array}
          cleaneddata.splice(0,1);

          //add the question data to another file 
          fs.appendFile( __dirname+"/users/" + req.signedCookies['name'] + ".txt",cleaneddata.toString() + "\r\n", function (err) {  //writes inside the temp file for the questions
            if (err) throw err;

            fs.createReadStream( __dirname+"/users/" + req.signedCookies['name'] + ".txt").pipe(fs.createWriteStream( __dirname+'/users.txt', {flags: 'a'}));

          fs.unlink( __dirname+"/users/"+ req.signedCookies['name'] + ".txt",function(err){
            if(err) return console.log(err);
            res.clearCookie("name");


          });
          });


        });


       } 
      });
    })
    } 


1 个答案:

答案 0 :(得分:0)

作为建议:

var lineReader = require('linebyline');

app.post('/return' , (req, res) => {

  var cookie = req.signedCookies['name'];

  // check cookie
  console.log(cookie);


  // red the UID file:
  rl = readline(__dirname + 'uid.txt');
  // BTW: Why no database?!

  rl.on('line', function(line, lineCount, byteCount) {
    if (line === cookie) {
      // ...
    }
  })
  .on('error', function(e) {
    // something went wrong
    res.status(500).json({
      error: err
    })
  });

  // Here you are filling some txt files with data out of your form data.. 
  // I highly suggest using a database instead of using your local directory
  // structure and txt files.

})