Following tutorial but get: Router.use() requires a middleware function but got a Object

I am following this tutorial but I am getting a problem, not sure if something has changed as the video is 2 years old.

I created a new file called posts.js

const express = require('express');

const router = express.Router();

router.get("/", (req, res) => {
    res.send("We are on posts");
  });

module.express = router;

then in my app.js

const postsRoutes = require('./routes/posts')


app.use('/posts', postsRoutes);

yet I get TypeError: Router.use() requires a middleware function but got a Object

Answer

Change module.express to module.exports. You are not correctly exporting the router, thus you cannot use it properly when calling .use

Leave a Reply

Your email address will not be published. Required fields are marked *