var http = require('http');
const multer = require('multer')
 
 //The disk storage engine gives you full control on storing files to disk.
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "")
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname)
    }
})
//and modify the upload variable to
var upload = multer({
    storage: storage,
}).single("filetoupload");
 
http.createServer(function (req, res) {
    if (req.url == '/fileupload') {
        upload(req,res,function(err){
            if(err)
            {
                res.end();
            }
            else
            {
                res.write("Success, File Uploaded!")
                res.end()
            }
        })
    } else {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
        res.write('<input type="file" name="filetoupload"><br>');
        res.write('<input type="submit">');
        res.write('</form>');
        return res.end();
    }
}).listen(3000);