Add json body parser; Add default response for errors

This commit is contained in:
Stefan-5422
2022-03-23 12:59:33 +01:00
parent b42f08bd8b
commit d7f97b49e7
4 changed files with 1273 additions and 57 deletions
+19 -1
View File
@@ -1,15 +1,33 @@
import express from "express";
import express, { NextFunction, Request, Response } from "express";
import dotenv from "dotenv";
import adminRoutes from "./routes/auth.route";
dotenv.config();
const app = express();
// Bodyparser and urlencoded to parse post request bodies
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
if (err) {
res.status(400).send({
type: "error",
payload: "The body of your request did not contain valid data",
});
} else {
next();
}
});
async function main() {
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.use("/api/auth", adminRoutes);
app.listen(3000, () => {
console.log("Server is running on port 3000");
});