Home > Software design >  First API using NODE.JS
First API using NODE.JS

Time:01-03

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.

CodePudding user response:

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!

CodePudding user response:

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
  • Related