how to upload image file and display using express nodejs : <p>I have used following code :</p>

<p>fileupload.html</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;File Uploading Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;File Upload:&lt;/h3&gt;
Select a file to upload: &lt;br /&gt;
&lt;form action="http://127.0.0.1:8081/file_upload" method="POST" enctype="multipart/form-data"&gt;
&lt;input type="file" name="file" size="50" /&gt;
&lt;br /&gt;
&lt;input type="submit" value="Upload File" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>fileupload.js:</p>

<pre><code>var express = require('express');
var app = express();
var fs = require("fs");

var bodyParser = require('body-parser');
var multer  = require('multer');

//console.log(multer);

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'}));

app.get('/fileupload.html', function (req, res) {
   res.sendFile( __dirname + "/" + "fileupload.html" );
})

app.post('/file_upload', function (req, res) {

   console.log(req.files.file.name);
   console.log(req.files.file.path);
   console.log(req.files.file.type);

   var file = __dirname + "/" + req.files.file.name;
   fs.readFile( req.files.file.path, function (err, data) {
        fs.writeFile(file, data, function (err) {
         if( err ){
              console.log( err );
         }else{
               response = {
                   message:'File uploaded successfully',
                   filename:req.files.file.name
              };
          }
          console.log( response );
          res.end( JSON.stringify( response ) );
       });
   });
})

var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port

  console.log("Example app listening at http://%s:%s", host, port)
})
</code></pre>

<p>I have used these above code for file uploading But when excute using node fileupload.js in terminal i am getting type error <code>TypeError('app.use() requires middleware functions');</code> </p>

<p>Can any one help to resolved this problem.</p>
