title: Change the name of uploaded file in node.js
body: <p>I am using <code>node.js</code> to upload a file. But it uploads the file in the <code>/tmp</code> folder with a random like name (such as: <code>132d439bb31ee13daaf6ce02e223738f</code>). I want the node to upload the file in a given directory with a given name. How can I make it? here is my code:</p>
  
  <pre><code>var http = require("http"),
      url = require("url"),
      sys = require("sys"),
      events = require("events"),
      fs = require("fs"),
      formidable = require('formidable'),
      util = require('util');
  
  var server = http.createServer(function(req, res) {
      switch (url.parse(req.url).pathname) {
          case '/':
              display_form(req, res);
              break;
          case '/upload':
              upload_file(req,res);
              break;
          default:
              show_404(req, res);
              break;
      }
  });
  
  server.listen(8124);
  
  function display_form(req, res) {
      //displays an html form with an upload and a submit button
  }
  
  function upload_file(req, res) {
      if (req.url == '/upload' &amp;&amp; req.method.toLowerCase() == 'post') {
  
        // Instantiate a new formidable form for processing.
  
        var form = new formidable.IncomingForm();
  
        // form.parse analyzes the incoming stream data, picking apart the different fields and files for you.
  
        form.parse(req, function(err, fields, files) {
          if (err) {
  
            // Check for and handle any errors here.
  
            console.error(err.message);
            return;
          }
              form.on('fileBegin', function(name, files) {
                  files.name="./guake.up";
              });
          res.writeHead(200, {'content-type': 'text/plain'});
          res.write('received upload:\n\n');
  
              console.log(files.name);
  
          // This last line responds to the form submission with a list of the parsed data and files.
  
          res.end(util.inspect({fields: fields, files: files}));
        });
        return;
      }
  }
  
  function show_404(req, res) {
      //shows a 404 page
  }
  </code></pre>