skip to Main Content

I am creating my first api and I am getting this error somehow.
ERROR

db.js

import { Pool } from 'pg';

const pool = new Pool ({
    user:"kethia",
    host:"localhost",
    database:"regionapi",
    password:"Kethia",
    port:5432,

});

export default pool;

controllers/src/routes.js

import express from 'express';
const router = express.Router();
//const router = Router();

router.get('/', (req,res) => {
    res.send("using api route");
})


export default pool;

server.js

import express from 'express';
import bodyParser from 'body-parser'; //This allows us to take in incoming post request bodies

import regionRoutes from './controllers/src/routes.js';
//const regionRoutes = require('./controllers/src/routes');


const app = express();
const PORT = 4500;

app.get('/', (req, res) => {
    console.log('[TEST]!');
    res.send('Hello from Homepage.')
})

app.use('/api/v1/regionApi', regionRoutes);

app.listen(PORT, () => console.log(`Server running on port:http://localhost:${PORT}`));

The thing is I don’t really get why I’m getting this error unless I missed something.

2

Answers


  1. The error really says it all – you don’t have a pool in your routes.js. Seems like you meant to export the router:

    import express from 'express';
    const router = express.Router();
    
    router.get('/', (req,res) => {
        res.send("using api route");
    })
    
    
    export default router; // Here!
    
    Login or Signup to reply.
  2. You are exporting the pool in the routes.js file but you didn’t define it.
    Instead of a pool, you have to write router because here you described routes

    import express from 'express';
    const router = express.Router();
    
    router.get('/', (req,res) => {
        res.send("using api route");
    })
    
    export default router; // line 10
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search