0

I am fairly new with node js. Right now I am running a web server where the client has to type their name in and a profile is made. However, I want the name the client typed to be written to an output.txt file. I know how to write hello world to an output text but not how to get a client's input to be directly written into an output text file.

    var http = require('http');
    var postHTML = 
    '<html><head><title>Post Example</title></head>' +
    '<body>' +
    '<form method="post">' +
    'Input 1: <input name="input1"><br>' +
    'Input 2: <input name="input2"><br>' +
    '<input type="submit">' +
    '</form>' +
    '</body></html>';

    http.createServer(function (req, res) {
     var body = "";
       req.on('data', function (chunk) {
      body += chunk;
      });
     req.on('end', function () {
       console.log('POSTed: ' + body);
      res.writeHead(200);
     res.end(postHTML);
      });
    }).listen(8080);

I want whatever the user types to input 1 to be saved to an output file

4
  • 1
    share your code please! Commented Jun 10, 2016 at 16:10
  • So, is the problem that you can't get get the value they typed in? Since you already know how to write to the file? Is this being typed into the console? Commented Jun 10, 2016 at 16:10
  • Yes i do not know how to get the value they typed in into output.txt Commented Jun 10, 2016 at 16:17
  • Like lets say you type "John" into placeholder on the server. I want this John to be written into output.txt Commented Jun 10, 2016 at 16:20

3 Answers 3

1

@patidar I am very new to node and I just made a solution. It may not give you the correct answer but an approach to proceed.

Hope this helps:

 var http = require('http');
 var qs = require('querystring');
 var fs = require('fs');
    var postHTML = 
    '<html><head><title>Post Example</title></head>' +
    '<body>' +
    '<form method="post">' +
    'Input 1: <input name="input1"><br>' +
    'Input 2: <input name="input2"><br>' +
    '<input type="submit">' +
    '</form>' +
    '</body></html>';

    http.createServer(function (req, res) {
     var body = "";
       req.on('data', function (chunk) {
      body += chunk;

      console.log(body);
      });
     req.on('end', function () {
       console.log('POSTed: ' + body);
       var post = qs.parse(body);
       console.log(post.input1);
       fs.writeFile("result_posted.txt",post.input1);
      res.writeHead(200);
     res.end(postHTML);
      });
    }).listen(8080);

It is writing the data into the file, but the problem is your code is posting the data twice so it is getting overwrited.

If anybody want to edit the post feel free to edit. Thanks.

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

Comments

0

You can read from the commandline with process.stdin. example below

var fs = require('fs');

var outFile = 'output.txt';

process.stdin.setEncoding('utf8');
process.stdin.on('readable', function(){
    var chunk = process.stdin.read();

    if(chunk !== null){
        fs.writeFile(outFile, chunk, 'utf-8', function(err){
            if(err){
                console.log(err);
            }
            process.exit();
        });
    }
});

1 Comment

@patidar see the document here nodejs.org/dist/latest-v4.x/docs/api/….
0

If you are reading from console, you can use streams to handle it all:

var fs = require('fs');

var output = fs.createWriteStream('output.txt');

process.stdin.pipe(output);

// this is optional, but good practice...
process.on('SIGINT', function(){
    output.end();
    process.exit();
});

If you are getting data from a form via HTTP or whatever, you'll need to provide some sample code or more information.

Comments

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.