title: Node.js upload files to server without additionals frameworks
body: <p>I'm writing a simple uploading website. I use <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest" rel="nofollow">XmlHTTPRequest</a> to upload files. Until now I've experienced only this part, with server already prepared for file uploading. But now I need to make my own server to test this web app locally. I've chosen Node.js, because it uses JavaScript and appears quite simple in comparison to other servers. But still, I'm not very experienced in using this, so I don't know, how to "catch" uploaded files from request and save them to some folder located in my PC. I've been trying to find a solution, but every one I've found is using some framework. I don't want to use these, if possible, because I don't want to add any complexity to my server code, since it's not the main point of my project and I need it really the simplest possible, just for testing purposes.</p>
  
  <p>So, could you please recommend me some easy way to do this? If you think, that "clear" Node.js without frameworks is not ideal, feel free to describe any other solution, I'll try my best to understand it :-)</p>
  
  <hr>
  
  <p>I've written the basic part of the server which print some statements and loads my source codes:</p>
  
  <pre><code>var http = require('http');
  var fs = require('fs');
  var path = require('path');
  
  http.createServer(function(request, response) {
      console.log(request.method + ' ' + request.url);
      var filePath = '.' + request.url;
      if (filePath == './')
          filePath = './index.html';
      if (filePath.indexOf('/../') !== -1) {
          response.writeHead(400);
          response.end();
      } else {
          var extname = path.extname(filePath);
          var contentType = 'text/plain';
          switch (extname) {
              case '.js': contentType = 'text/javascript'; break;
              case '.html': contentType = 'text/html'; break;
              case '.css': contentType = 'text/css'; break;
          }
          fs.exists(filePath, function(exists) {
              if (exists) {
                  fs.readFile(filePath, function(error, content) {
                      if (error) {
                          response.writeHead(500);
                          response.end();
                      } else {
                          response.writeHead(200, { 'Content-Type': contentType });
                          response.end(content, 'utf-8');
                      }
                  });
              } else {
                  response.writeHead(404);
                  response.end();
              }
          });
      }
  }).listen(8080);
  </code></pre>