作者 | SHA1 | 備註 | 提交日期 |
---|---|---|---|
James Peret | 0eac3d3519 | 7 年之前 | |
James Peret | 7cfdc92245 | 7 年之前 |
@@ -50,6 +50,44 @@ var getPath = function(req){ |
||
50 | 50 |
return path; |
51 | 51 |
} |
52 | 52 |
|
53 |
+var getIndexFilePath = function(path, req, res){ |
|
54 |
+ if(req.params['file'] != undefined){ |
|
55 |
+ path = path + req.params['file'] + "/"; |
|
56 |
+ } |
|
57 |
+ console.log("Looking for index file in " + path); |
|
58 |
+ fs.readFile(path + "index.html", 'utf8', function (err,data) { |
|
59 |
+ if (err) { |
|
60 |
+ fs.readFile(path + "index.md", 'utf8', function (err,data) { |
|
61 |
+ if (err) { |
|
62 |
+ error_404(req, res); |
|
63 |
+ } else { |
|
64 |
+ req.params['file'] = buildFilename(req.params['file'], "index.md"); |
|
65 |
+ get_file(req, res); |
|
66 |
+ } |
|
67 |
+ }); |
|
68 |
+ } else { |
|
69 |
+ req.params['file'] = buildFilename(req.params['file'], "index.html"); |
|
70 |
+ get_file(req, res); |
|
71 |
+ } |
|
72 |
+ }); |
|
73 |
+} |
|
74 |
+ |
|
75 |
+var isFile = function(filename){ |
|
76 |
+ var re = /(?:\.([^.]+))?$/; |
|
77 |
+ var ext = re.exec(filename)[1]; |
|
78 |
+ if(ext != undefined){ |
|
79 |
+ return true; |
|
80 |
+ } |
|
81 |
+ return false; |
|
82 |
+} |
|
83 |
+ |
|
84 |
+var buildFilename = function(path, filename){ |
|
85 |
+ if(path == undefined){ |
|
86 |
+ return filename; |
|
87 |
+ } |
|
88 |
+ return path + "/" + filename |
|
89 |
+} |
|
90 |
+ |
|
53 | 91 |
var get_file = function(req, res){ |
54 | 92 |
|
55 | 93 |
var path = getPath(req); |
@@ -58,7 +96,7 @@ var get_file = function(req, res){ |
||
58 | 96 |
var extension; |
59 | 97 |
|
60 | 98 |
// Load file or look for index? |
61 |
- if(req.params['file'] != undefined){ |
|
99 |
+ if(isFile(req.params['file'])){ |
|
62 | 100 |
// Set filetype |
63 | 101 |
path = path + req.params['file']; |
64 | 102 |
parts = req.params['file'].split("."); |
@@ -71,6 +109,8 @@ var get_file = function(req, res){ |
||
71 | 109 |
} |
72 | 110 |
} else { |
73 | 111 |
// return index file if it exists |
112 |
+ path = getIndexFilePath(path, req, res) |
|
113 |
+ return; |
|
74 | 114 |
} |
75 | 115 |
// Action for each filetype |
76 | 116 |
if(file_type == "markdown"){ |
@@ -108,7 +148,7 @@ var write_file = function(req, res){ |
||
108 | 148 |
var path = getPath(req); |
109 | 149 |
var save_data; |
110 | 150 |
// Write file |
111 |
- if(req.params['file'] != undefined){ |
|
151 |
+ if(isFile(req.params['file'])){ |
|
112 | 152 |
path = path + req.params['file']; |
113 | 153 |
if(path == ""){ |
114 | 154 |
console.log("Error: path not specified") |
@@ -134,6 +174,10 @@ var write_file = function(req, res){ |
||
134 | 174 |
} |
135 | 175 |
} |
136 | 176 |
|
177 |
+var error_404 = function(req, res){ |
|
178 |
+ res.render('error-404', extra_data()); |
|
179 |
+} |
|
180 |
+ |
|
137 | 181 |
var markdown_parser = function(data){ |
138 | 182 |
var md = require('markdown-it')({ |
139 | 183 |
html: true, |
@@ -164,6 +208,7 @@ var extra_data = function(){ |
||
164 | 208 |
} |
165 | 209 |
|
166 | 210 |
var url_paths = [ |
211 |
+ '/', |
|
167 | 212 |
'/:file', |
168 | 213 |
'/:folder_1/:file', |
169 | 214 |
'/:folder_2/:folder_1/:file', |
@@ -1,6 +1,6 @@ |
||
1 | 1 |
{ |
2 | 2 |
"name": "@jamesperet/codex-server", |
3 |
- "version": "0.0.4", |
|
3 |
+ "version": "0.1.0", |
|
4 | 4 |
"description": "A node file server with super powers.", |
5 | 5 |
"main": "index.js", |
6 | 6 |
"scripts": { |
@@ -1,11 +1,26 @@ |
||
1 | 1 |
# Codex Server |
2 | 2 |
|
3 |
-*Create by James Peret* |
|
4 |
- |
|
5 | 3 |
Codex is a markdown wiki server with mathjax and syntax highlighting. |
6 | 4 |
|
5 |
+### How it works |
|
6 |
+ |
|
7 |
+The codex server works as web server for local files inside a directory and its subdirectorys. Codex will render each filetype with a nice customizable webpage template. |
|
8 |
+ |
|
7 | 9 |
### Starting |
8 | 10 |
|
9 | 11 |
To start the server, navigate to the root folder of your file hierarchy and run the command: ```codex``` to start the program. |
10 | 12 |
|
11 | 13 |
Press ```Ctrl + c``` to stop the process. |
14 |
+ |
|
15 |
+### Templating |
|
16 |
+ |
|
17 |
+For nice looking pages, you will need to place a templates folder called ```views/``` in the root of your hierarchy. In this folder place ```.html``` or ```.ejs``` files for each type of template: |
|
18 |
+ |
|
19 |
+- ```index.html``` - This is the main template for rendering most pages (like markdown). Add ```<%- body %>``` somewhere in this file for rendering content passed from codex. |
|
20 |
+- ```error-404.html``` - A basic error page template. |
|
21 |
+ |
|
22 |
+Restarting the server is not required when changin templates. |
|
23 |
+ |
|
24 |
+--- |
|
25 |
+ |
|
26 |
+*Codex Server – create by James Peret – 2017* |