noteshare.space/server/server.ts

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-06-22 00:30:11 +03:00
import "dotenv/config";
import express, { Express, Request, Response } from "express";
2022-06-23 21:22:34 +03:00
import cors from "cors";
2022-06-22 00:30:11 +03:00
import { PrismaClient, EncryptedNote } from "@prisma/client";
const prisma = new PrismaClient();
const app: Express = express();
2022-06-23 21:22:34 +03:00
if (process.env.ENVIRONMENT == "dev") {
app.use(
cors({
origin: "*",
})
);
}
2022-06-22 00:30:11 +03:00
app.use(express.json());
// start the Express server
app.listen(process.env.PORT, () => {
console.log(`server started at http://localhost:${process.env.PORT}`);
});
// Post new encrypted note
app.post("/note/", async (req: Request<{}, {}, EncryptedNote>, res) => {
const note = req.body;
const savedNote = await prisma.encryptedNote.create({ data: note });
res.json({ view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}` });
});
// Get encrypted note
app.get("/note/:id", async (req, res) => {
const note = await prisma.encryptedNote.findUnique({
where: { id: req.params.id },
});
if (note != null) {
res.send(note);
}
res.status(404).send();
});
// Default response for any other request
app.use((req, res, next) => {
res.status(404).send();
});