nodejs throw new TypeError('app.use() requires a middleware function') code example

Example: TypeError: app.use() requires a middleware function

const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());

// tells express which template engine to use
app.set('view engine', 'pug');

const routes = require('./routes');

app.use(routes);

app.use((req, res, next) => {
    console.log('Hello');
    next();
});
app.use((req, res, next) => {
    console.log("World");
    next();
});

app.use((req, res, next) => {
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});

app.use((err, req, res, next) => {
    res.locals.error = err;
    res.status(err.status);
    res.render('error');
});

app.listen(3000, () => {
    console.log('The application is running on localhost 3000');
});

Tags:

Go Example