forked from ShawonAshraf/LocalFileServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (38 loc) · 1.09 KB
/
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
41
42
43
44
45
46
47
import express from "express"
import hbs from "hbs"
import morgan from "morgan"
import { gatherFiles, hbsTableListing } from "./utils/file-listing"
// init
let app = express()
const port = 9000
// register middleware
app.set("view engine", hbs)
app.use(morgan("combined"))
// register hbs helper
hbs.registerHelper("list", hbsTableListing)
hbs.registerPartials(__dirname + "/views/partials")
// static dir for files
app.use("/files", express.static(__dirname + "/files"))
// public dir
app.use(express.static(__dirname + "/public"))
// replace anonymous with your preferred name
app.get("/", (req, res) => {
let path = "files"
gatherFiles(path)
.then(files => {
res.render("index.hbs", {
files: files,
title: "Anonymous's Local File Server"
})
}).catch(err => {
res.render("bad.hbs", {
err: err,
title: "Anonymous's Local File Server"
})
})
})
app.listen(port, () => {
console.log(`Server Running @ PORT :: ${port}`)
})
// export for testing
export default app