-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·61 lines (55 loc) · 1.49 KB
/
index.js
File metadata and controls
executable file
·61 lines (55 loc) · 1.49 KB
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
/**
* Import Modules
*/
const express = require("express");
const app = express();
const path = require("path");
var util = require("util");
var fs = require("fs");
var process = require("process");
/**
* Variable Declaration
*/
var public = "public";
/**
* Fetching files in the public folder
*/
fs.readdir(public, function(err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(function(file, index) {
// Make one pass and make the file complete
if (file.indexOf(".html") > 0) {
var filepath = path.join(public, file);
var route = filepath.replace(".html", "").replace(public + "/", "");
getFileStat(filepath,route);
}
});
});
function getFileStat(filepath,route){
fs.stat(filepath, function(error, stat) {
if (error) {
console.error("Error stating file.", error);
return;
}
if (stat.isFile()) {
createRoute(route, filepath);
console.log("'%s' is a file.", filepath);
}
});
}
function createRoute(route, filepath) {
filepath = "/" + filepath;
/* ****************** Routes ******************* */
app.get("/" + route, function(req, res) {
res.sendFile(path.join(__dirname + filepath));
});
}
/**************** Run Application*************/
app.set("port", process.env.PORT || 5000);
app.use(express.static(__dirname + "/public"));
app.listen(app.get("port"), function() {
console.log("App is running, server is listening on port ", app.get("port"));
});