-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (27 loc) · 882 Bytes
/
server.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
38
39
40
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import posts from "./routes/posts.js";
import logger from "./middleware/logger.js";
import errorHandler from "./middleware/error.js";
import notFound from "./middleware/notFound.js";
const port = process.env.PORT || 8000;
// Get the directory name
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// body parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// logger middleware
app.use(logger);
// setup static folder
app.use(express.static(path.join(__dirname, "public")));
// Routes
app.use("/api/posts", posts);
// error handler middleware
app.use(notFound);
app.use(errorHandler);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});