server.js

const express = require('express');
const connectDB = require('./config/db');

const app = express();

//connect database
connectDB();

app.get(`/`, (req, res) => res.send('API Running'));

// Define Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/posts', require('./routes/api/posts'));
app.use('/api/profile', require('./routes/api/profile'));
app.use('/api/auth', require('./routes/api/auth'));

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`server started on port ${PORT}`));

users.js file .... auth profiles and posts are all pretty much the same with users replaced with corresponding keywords

const express = require('express');
const router = express.Router();
// @route   GET api/users
// @desc    Test route
// @access  Public
router.get('/', (req, res) => res.send('users route'));

module.exports = router;

i get an error

[nodemon] starting `node server.js`
F:\Project\Workspace\Starter project\node_modules\express\lib\router\index.js:458
      throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object
    at Function.use (F:\Project\Workspace\Starter project\node_modules\express\lib\router\index.js:458:13)
    at Function.<anonymous> (F:\Project\Workspace\Starter project\node_modules\express\lib\application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (F:\Project\Workspace\Starter project\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (F:\Project\Workspace\Starter project\server.js:14:5)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782: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...

I'm very new to node and express please explain or give link to documents which explains from the basic ^_^ here is the profile.js file

const express = require('express');
const router = express.Router();
// @route   GET api/profile
// @desc    Test route
// @access  Public
router.get('/', (req, res) => res.send('profile route'));

module.exports = router;
1

There are 1 answers

0
Genocide On

Turns out it was a problem with my vscode extensions had to disable a eslint and react snippets and voila its working