user2325154 user2325154 - 6 months ago 27
Node.js Question

Not able to execute a fileupload program in Nodejs

I have a simple program that I am trying to execute from online tutorial at but I am getting an exception as follows:

I am relatively new to nodejs and any sort of help or guidance will be much appreciated.

Using NodeJS version: 4.1.1.


TypeError: app.use() requires middleware functions
at EventEmitter.use (c:\nital\my-data\my-sample-apps\nodejs\tutorialspoint-samples\node_modules\express\lib\application.js:209:11)
at Object.<anonymous> (c:\nital\my-data\my-sample-apps\nodejs\tutorialspoint-samples\Sample37.js:11:5)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3


var express = require('express');
var app = express();
var fs = require("fs");

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

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

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

//uploading the file here'/file_upload', function (req, res) {

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

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)


It looks like the api of multer has changed. Since the tutorial uses npm install multer --save it pulls in the latest version rather than making sure the version you have is compatible. Details on how to use it can be found on the npm page. It seems you now need to add the particular middleware you want into the specific route:

// Remove this line: app.use(multer({dest: 'C:/tmp/'}));
// Put this in instead
var upload = multer({ dest: 'C:/tmp/' });

// Change this:'/file_upload', function (req, res) {
// to'/file_upload', upload.single('file'), function (req, res) {

Though you could probably still use app.use to add it to all routes, you definitely need to change it to use upload.single (or another of their choice of middlewares).