How to store images to MongoDB with Node.js
Alven Wang

Hi, thanks for the code, I’m actually new to nodejs so I try to run the code but I’m having this error : throw new TypeError(‘app.use() requires middleware functions’); I’m using express 4.x.x. please can you give me some hints? Thanks!

My code : var bodyParser = require(‘body-parser’);
var multer = require(‘multer’);
var hbs = require(‘express-handlebars’);
var validator = require(‘express-validator’);
var session = require(‘express-session’);
var passport = require(‘passport’);
var flash = require(‘connect-flash’);
var bcrypt = require(‘bcrypt-nodejs’);

var routes = require(‘./routes/index’);
var users = require(‘./routes/users’);
var admin = require(‘./routes/admin’);
var cart = require(‘./routes/cart’);

var app = express();

app.engine(‘hbs’, hbs({extname : ‘hbs’, defaultLayout: ‘layout’, layoutsDir: __dirname + ‘/views/layouts/’,partialsDir: __dirname + ‘/views/partials/’}));
app.set(‘views’, path.join(__dirname, ‘views’));
app.set(‘view engine’, ‘hbs’);

app.use(multer({ dest: ‘public/images/’,
 rename: function (fieldname, filename) { return filename; }}));

app.use(favicon(path.join(__dirname, ‘public’, ‘favicon.ico’)));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({secret : ‘anything’, resave: false, saveUninitialized: false}));

Show your support

Clapping shows how much you appreciated Sam Alfred’s story.