api.expres-ts
Version:
ExpressJS with Typescript project
34 lines (28 loc) • 938 B
text/typescript
import express, { json, urlencoded, Express, Request, Response } from "express";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const allowedOrigins = ["http://localhost:3000"];
const corsOptions = {
origin: allowedOrigins,
};
const app: Express = express();
app.use(cors(corsOptions));
app.use(json({ limit: "500kb" }));
app.use(urlencoded({ limit: "500kb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "500kb", extended: false }));
const port = process.env.PORT;
if (!process.env.PORT) {
throw new Error("Port not found");
}
let count = 0;
app.get("/", (req: Request, res: Response) => {
res.json({ count: count });
});
app.get("/add", (req: Request, res: Response) => {
res.json({ count: count++ });
});
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});