-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (54 loc) · 1.48 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import express from "express";
import debug from "debug";
import cors from "cors";
import dotenv from "dotenv";
import connectMongoDB from "./config/mongodb-connection.config.js";
import fs from "fs";
dotenv.config();
connectMongoDB();
const app = express();
const debugging = debug("development:app");
import taskRouter from "./routers/task.router.js";
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(cors());
app.use("/api/task", taskRouter);
app.get("/", (req, res) => {
fs.readdir("./files", (err, files) => {
res.render("index", { files: files });
debugging(files);
});
});
app.get("/tasks/:taskname", (req, res) => {
fs.readFile(`./files/${req.params.taskname}`, "utf-8", (err, data) => {
if (err) {
return res.status(500).render("error", {
error: err.message,
});
} else {
return res.render("task", {
taskName: req.params.taskname,
taskDetails: data,
});
}
});
});
app.get("/edit/:filename", (req, res) => {
fs.readFile(`./files/${req.params.filename}`, "utf-8", (err, data) => {
if (err) {
return res.status(500).render("error", {
error: err.message,
});
} else {
return res.render("edit", {
filename: req.params.filename,
taskDetails: data,
});
}
});
});
app.listen(process.env.PORT, () => {
debugging(`server is running at port: ${process.env.PORT}`);
});