-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (30 loc) · 1012 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const errorHandler = require("./middlewares/errorHandler");
const logger = require("./middlewares/logger");
const authController = require("./controllers/authController");
const demoRoute = require("./routes/demo");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5050;
const corsOptions = {
origin: process.env.CORS_ORIGIN || "*",
methods: ["GET", "POST", "PUT", "DELETE"],
optionsSuccessStatus: 200,
};
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors(corsOptions));
app.use(logger); // Logging middleware
// Routes
app.get("/", (req, res) => {
res.json({ info: "Node.js, Express, and Postgres API" });
});
app.use("/demo", authController.verifyToken, demoRoute);
// Error handling middleware
app.use(errorHandler);
app.listen(port, () => {
console.log(`App running on port ${port}.`);
});