0

I know how I can simply write a file using nodes File System. However, I have a simple HTML page with an input. I want to use javascript to get the value of the input submitted by a user and take this value and add it into the file written by node.

For example:

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'This is my text' + <VARIABLE>, function (err) {
  if (err) throw err;
  console.log('Replaced!');
});

Where is the value being pulled in from the javascript on the input.

1
  • 2
    Create a route for a POST request, add the variable to the body of the POST request and call this function when the route is hit. Commented Jun 3, 2019 at 18:51

1 Answer 1

1

Assuming you are using Express, the req object has a property called query that allows you to access the parameters sent with a request.

app.get('/myroute/', function(req, res) {
   console.log(JSON.stringify(req.query)); //log entire query object
   fs.writeFile('mynewfile3.txt', 'This is my text' + req.query.myvariable, function(err) {
        if (err) throw err;
        console.log('Replaced!');
        res.json({
                    success: true,
                    message: "File written"
                });
   });
});
Sign up to request clarification or add additional context in comments.

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.