v1.0.0 #28
81
src/index.ts
81
src/index.ts
|
@ -7,55 +7,56 @@ export const app = express();
|
|||
app.use(express.json());
|
||||
|
||||
app.get("/", (_, res) => {
|
||||
res.json({ message: "Blazing fast 🚀" });
|
||||
res.json({ message: "Blazing fast 🚀" });
|
||||
});
|
||||
|
||||
app.get("/images", async (req, res) => {
|
||||
try {
|
||||
const allImages = await ImageModel.find();
|
||||
res.json(allImages);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error });
|
||||
}
|
||||
})
|
||||
app.get("/images", async (_, res) => {
|
||||
try {
|
||||
const allImages = await ImageModel.find();
|
||||
res.json({ message: allImages });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: error });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/images", async (req, res) => {
|
||||
try {
|
||||
// Should add auth here before doing stuff
|
||||
// Thowing a 401 if not auth provided
|
||||
// Throwing a 403 for incorrect auth
|
||||
const image = await ImageModel.create(req.body);
|
||||
res.status(201).json(image);
|
||||
} catch (error: any) {
|
||||
if (error.code == 11000){
|
||||
// Should return 409 Conflict for existing urls
|
||||
res.status(409).json({ message: "Existing URL" });
|
||||
}
|
||||
// Should return 400 Bad request for invalid requests
|
||||
res.status(400).json({ message: error });
|
||||
try {
|
||||
// Should add auth here before doing stuff
|
||||
// Thowing a 401 if not auth provided
|
||||
// Throwing a 403 for incorrect auth
|
||||
const image = await ImageModel.create(req.body);
|
||||
res.status(201).json({ message: image });
|
||||
} catch (error: any) {
|
||||
if (error.code == 11000) {
|
||||
// Should return 409 Conflict for existing urls
|
||||
res.status(409).json({ message: "Existing URL" });
|
||||
}
|
||||
})
|
||||
// Should return 400 Bad request for invalid requests
|
||||
res.status(400).json({ message: error });
|
||||
}
|
||||
});
|
||||
|
||||
// Set the default port to 8080, or use the PORT environment variable
|
||||
|
||||
const start = async () => {
|
||||
const port = process.env.PORT || 8080;
|
||||
const mongo_uri: string = process.env.MONGODB_URI || "";
|
||||
const mongo_user = process.env.MONGODB_USER;
|
||||
const mongo_pass = process.env.MONGODB_PASS;
|
||||
const port = process.env.PORT || 8080;
|
||||
const mongo_uri: string = process.env.MONGODB_URI || "";
|
||||
const mongo_user = process.env.MONGODB_USER;
|
||||
const mongo_pass = process.env.MONGODB_PASS;
|
||||
|
||||
try {
|
||||
await mongoose.connect(mongo_uri, {
|
||||
authSource: "admin",
|
||||
user: mongo_user,
|
||||
pass: mongo_pass
|
||||
});
|
||||
app.listen(port, () => console.log(`Express server listening on port ${port}`));
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await mongoose.connect(mongo_uri, {
|
||||
authSource: "admin",
|
||||
user: mongo_user,
|
||||
pass: mongo_pass,
|
||||
});
|
||||
app.listen(port, () =>
|
||||
console.log(`Express server listening on port ${port}`)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
start();
|
||||
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
import { beforeAll, describe, expect, it } from "bun:test";
|
||||
import { describe, expect, it, mock } from "bun:test";
|
||||
import request from "supertest";
|
||||
import { app } from "../src";
|
||||
|
||||
describe("GET / shows what it should",async () => {
|
||||
const res = await request(app).get("/");
|
||||
|
||||
it("should be", async () => {
|
||||
expect(res.body).toHaveProperty("message", "Blazing fast 🚀");
|
||||
});
|
||||
})
|
||||
|
||||
describe("GET /images works properly", async () => {
|
||||
const res = await request(app).get("/images");
|
||||
|
||||
it("should be an array", async () => {
|
||||
expect(Array.isArray(res.body)).toBeTrue();
|
||||
expect(Array.isArray(res.body.message)).toBeTrue();
|
||||
});
|
||||
|
||||
it("should return a 200", async () => {
|
||||
|
|
Loading…
Reference in New Issue