Skip to content

Commit

Permalink
Add /posts route
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed May 20, 2023
1 parent cae8417 commit d3982da
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
31 changes: 31 additions & 0 deletions __test__/routes/posts.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import supertest from "supertest";
import app from "../../src/app";
import mongoose from "mongoose";
import Post from "../../src/models/post.model";
import { env } from "../../src/config";

beforeEach(async () => {
mongoose.set("strictQuery", true);
await mongoose.connect(env.MONGODB_URL);
});

afterEach(async () => {
await mongoose.connection.db.dropDatabase();
await mongoose.connection.close();
});

describe("/posts routes", () => {
test("GET /posts", async () => {
const post = await Post.create({
title: "Post 1",
content: "Lorem ipsum",
});

const res = await supertest(app).get("/posts").expect(200);

expect(res.body.length).toEqual(1);
expect(res.body[0]._id).toBe(post.id);
expect(res.body[0].title).toBe(post.title);
expect(res.body[0].content).toBe(post.content);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "npx tsc",
"start": "node dist/index.js",
"dev": "env-cmd nodemon ./src/index.ts",
"test": "jest",
"test": "env-cmd jest",
"prettier": "npx prettier --write .",
"lint": "npx eslint ."
},
Expand Down
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import express, { type Express, type Request, type Response } from "express";
import postsRoute from "./routes/posts.route";

const app: Express = express();

app.use("/posts", postsRoute);

app.get("/", (req: Request, res: Response) => {
res.send("Express + TypeScript Server");
});
Expand Down
2 changes: 1 addition & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import Joi from "joi";

const schema = Joi.object()
Expand All @@ -21,7 +22,6 @@ if (error != null) {
throw new Error(`Config validation error: ${error.message}`);
}

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const MONGODB_URL = `mongodb://${env.MONGODB_USER}:${env.MONGODB_PASS}@${
env.MONGODB_HOST
}:${env.MONGODB_PORT}/${
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import mongoose from "mongoose";
import app from "./app";
import { env } from "./config";
Expand All @@ -11,7 +12,6 @@ main().catch((err) => {
async function main(): Promise<void> {
await mongoose.connect(env.MONGODB_URL);
app.listen(env.PORT, () => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.log(
`⚡️[server]: Server is running at http://localhost:${env.PORT}`
);
Expand Down
9 changes: 9 additions & 0 deletions src/routes/posts.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from "express";
import { getPosts } from "../controllers/posts";

const router = express.Router();

// eslint-disable-next-line @typescript-eslint/no-misused-promises
router.get("/", getPosts);

export default router;

0 comments on commit d3982da

Please sign in to comment.