I am trying to make a simple GET route with express to retrieve all posts from my posts table. All queries work with my seed file init_db.js, but when I try Thunderclient or the browser it just never resolves and no errors are shown. I have used console logs in my db/models/posts.js and it hits console.log('Beginning to get all posts') but never hits the console log afterwards.
Below is some of my code. I have omitted some extraneous code from these files for clarity and legibility.
api/posts.js:
const postsRouter = require('express').Router()
const { createPost, getAllPosts } = require('../db/models/posts')
postsRouter.use((req, res, next) => {
console.log(`You've hit the Posts Router`)
next()
})
postsRouter.get('/', async (req, res) => {
try {
const posts = await getAllPosts()
res.send(posts)
} catch(error) {
console.log(error)
}
})
module.exports = {
postsRouter
}
db/models/posts.js
const client = require("../client");
async function createPostsTable() {
try {
await client.query(`
CREATE TABLE posts
(
id SERIAL PRIMARY KEY,
title VARCHAR(255) UNIQUE NOT NULL,
content TEXT NOT NULL,
"videoURL" TEXT,
"videoTitle" VARCHAR(255),
"videoDescription" TEXT,
active BOOLEAN DEFAULT true
);
`);
} catch (error) {
console.log("Error building posts table!");
throw error;
}
}
async function getAllPosts() {
console.log('Beginning to get all posts')
try {
const { rows: posts} = await client.query(`
SELECT *
FROM posts;
`)
console.log(posts)
return posts
} catch(error) {
console.log('Error getting all posts')
throw error
}
}
module.exports = {
createPostsTable,
createPost,
createInitialPosts,
getAllPosts
};
index.js:
const express = require("express");
const app = express();
const PORT = 8080;
const cors = require("cors");
app.use(cors());
app.use(express.json());
const morgan = require("morgan");
app.use(morgan('dev'));
const path = require("path");
app.use(express.static(path.join(__dirname, "build")));
app.use("/", (req, res, next) => {
console.log('Body Logger Begin-->')
console.log(req.body)
console.log('Body Logger End-->')
next()
});
app.use("/api", require("./api"));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.listen(
PORT,
() => {
console.log(`Server is listening on PORT ${PORT}`);
}
);
api/index.js:
const apiRouter = require('express').Router();
const { postsRouter } = require('./posts')
apiRouter.get('/', (req, res, next) => {
res.send({
message: 'API is under construction!',
});
});
apiRouter.get('/health', (req, res, next) => {
res.send({
healthy: true,
});
});
apiRouter.use('/posts', postsRouter)
// place your routers here
module.exports = apiRouter;
And my seed file which is able to successfully make queries init_db.js:
const {
client,
} = require('./')
const {
createPostsTable,
createInitialPosts,
getAllPosts,
} = require('./models/index')
async function dropTables() {
await client.query(`
DROP TABLE IF EXISTS videos;
DROP TABLE IF EXISTS posts;
`)
}
async function buildTables() {
try {
client.connect();
console.log('Starting to drop tables')
await dropTables();
console.log('Finished dropping tables')
console.log('Starting to build tables')
await createPostsTable();
console.log('Finished building tables')
} catch(error) {
console.log("Error building tables")
throw error;
}
}
async function populateInitialData() {
try {
console.log('Starting to populate initial data')
await createInitialPosts()
console.log('Finished populating initial data')
} catch(error) {
console.log("Error populating initial data!")
throw error
}
}
async function testDB() {
try {
console.log('Starting to test the database')
console.log(await getAllPosts())
console.log('Finished testing the database')
} catch(error) {
console.log('Error testing database')
throw error
}
}
buildTables()
.then(populateInitialData)
.then(testDB)
.catch(console.error)
.finally(() => client.end())