this is the example code:
app.use logger 'dev'
app.use bodyParser.json()
app.use bodyParser.urlencoded()
app.use multer inMemory: yes
when it executes, throw an TypeError from app.use multer: inMemory: yes:
TypeError: app.use() requires middleware functions
at EventEmitter.use (/Users/ran/github/lbs-app/node_modules/express/lib/application.js:206:11)
at Object.<anonymous> (/Users/ran/github/lbs-app/app.js:69:7)
at Object.<anonymous> (/Users/ran/github/lbs-app/app.js:108:4)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/ran/github/lbs-app/bin/www:7:11)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:124:16)
I was confused at first, because the same code works well on my laptap, and when I check the dependencies' version, I found multer: 0.1.8 => 1.0.1...
So I'm thinking you had the same problem as me.
I had my multer configuration in my express config file, so now I'm rushing a fix to get it nested in to a route.
for example: in my routes.js file
module.exports = function(app) {
<-SNIP->
app.use('/api/uploads', require('./api/upload'));
<-SNIP->
};
and in /api/uploads/index.js
'use strict';
var express = require('express');
var controller = require('./upload.controller');
var multer = require('multer');
var os = require('os');
var uuid = require('node-uuid');
var storage = multer.diskStorage({
destination: function (req, file, cb) { cb(null, os.tmpdir()) },
filename: function (req, file, cb) { cb(null, uuid.v4());}
});
// Create the multer instance here
var upload = multer({ storage: storage, limits: { fileSize: 10 * 1024 * 1024}});
var router = express.Router();
// File Upload route
router.post('/', upload.single('file'), controller.upload);
module.exports = router;
I'm still finishing up my code, but that's a rough idea of what the change is.
Good luck!
@abbshr The syntax for using multer has changed with version 1.0.0, please see the readme on how to use it now.
You shouldn't have been updated automatically thought as this was a major version bump. If your dependencies contains "multer": "*" I strongly urge you to set it to "multer": "^1.0.1" instead. Else you will get the same problem with all backwards incompatible changes that we release.
@macneib That looks good! :+1:
The disk storage defaults the destination to os.tmpdir() so you could drop that if you want to. It also defaults to generating names with 16 bytes of random data as a hex-string (e.g. a087fda2cf19f341ddaeacacab285acc), if this works for you you could drop node-uuid and use the storage as this:
var storage = multer.diskStorage({})
var limits = { fileSize: 10 * 1024 * 1024 }
var upload = multer({ storage: storage, limits: limits })
That doesn't seem related to multer at all, but I think that the problem is that ss_data is an object instead of a function. What's in routes/uiData.js?
var express = require('express');
var app = express();
var dataFile = require('./data/data.json');
app.set('port', process.env.PORT || 3000);
app.use(require('./routes/index'));
var server = app.listen(app.get('port'), function() {
console.log('Listening on port '+app.get('port'));
});
error is
H:expressnode_modulesexpresslibapplication.js:210
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (H:expressnode_modulesexpresslibapplication.js:210:11)
at Object.
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
@sagarpandav Could it be that you have forgot to do module.exports = in your routes/index.js file?
@LinusU Thanks bro.. you solved my problem...
Index.js:
const express = require('express');
const socketio = require('socket.io');
const http = require('http');
const PORT = process.env.PORT || 5000;
const router = require('./router');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(router);
server.listen(PORT, () => console.log(Server has started on port ${PORT}));
Router.js:
const express = require('express');
const router = express.Router();
router.get(/, (req, res) => {
res.send('server is up and running');
});
module.export = router;
This throws an error that I dont know how to fix can someone help me?
throw new TypeError('app.use() requires a middleware function')
^
TypeError: app.use() requires a middleware function
at Function.use (/Users/Andres/Desktop/chat-app-2/server/node_modules/express/lib/application.js:210:11)
at Object.
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...
Most helpful comment
@sagarpandav Could it be that you have forgot to do
module.exports =in yourroutes/index.jsfile?