Saving files and data

James Peret 6 years ago
parent
commit
f5d9ba7212
2 changed files with 52 additions and 2 deletions
  1. 50 2
      index.js
  2. 2 0
      package.json

+ 50 - 2
index.js

@@ -6,11 +6,14 @@ var express = require('express')
6 6
 var app = express()
7 7
 var path = require('path');
8 8
 var fs = require('fs');
9
+var bodyParser = require('body-parser');
10
+var _ = require('lodash');
9 11
 
10 12
 app.engine('html', require('ejs').renderFile);
11 13
 app.set('view engine', 'html');
12 14
 //app.use('/public', express.static(path.join(__dirname + '/node_modules')));
13 15
 app.use(express.static('public'))
16
+app.use(bodyParser.json());
14 17
 
15 18
 var getPath = function(req){
16 19
   var path = "";
@@ -71,13 +74,52 @@ var get_file = function(req, res){
71 74
     fs.readFile(path, 'utf8', function (err,data) {
72 75
       if (err) {
73 76
         return console.log(err);
77
+      } else {
78
+        console.log("Rendered: " + path)
79
+        res.render('index', { body: markdown_parser(data) });
74 80
       }
75
-      res.render('index', { body: markdown_parser(data) });
76 81
     });
77 82
   } else {
78
-    res.sendFile( process.cwd() + "/" + path)
83
+    res.sendFile( process.cwd() + "/" + path, function (err) {
84
+      if (err) {
85
+        console.log("Error: " + path + " - file not found");
86
+        res.status(err.status).end();
87
+      }
88
+      else {
89
+        console.log('Sent:', path);
90
+      }
91
+    });
79 92
   }
93
+}
80 94
 
95
+var write_file = function(req, res){
96
+  var path = getPath(req);
97
+  var save_data;
98
+  // Write file
99
+  if(req.params['file'] != undefined){
100
+    path = path + req.params['file'];
101
+    if(path == ""){
102
+      console.log("Error: path not specified")
103
+      res.status(400).end();
104
+      return;
105
+    }
106
+    if (req.body.file != undefined) {
107
+      save_data = req.body.file
108
+    }
109
+    else if(req.body.data != undefined){
110
+      save_data = JSON.stringify(req.body.data)
111
+    }
112
+    fs.writeFile(path, save_data, function(err) {
113
+      if(err) {
114
+          console.log("Error: " + path + " - file could not be saved")
115
+          console.log(err);
116
+          res.status(err.status).end();
117
+      } else {
118
+        console.log("Saved: " + path);
119
+        res.status(200).end();
120
+      }
121
+    });
122
+  }
81 123
 }
82 124
 
83 125
 var markdown_parser = function(data){
@@ -114,6 +156,12 @@ for (var i = 0; i < url_paths.length; i++) {
114 156
   })
115 157
 }
116 158
 
159
+for (var i = 0; i < url_paths.length; i++) {
160
+  app.post(url_paths[i], function (req, res) {
161
+    write_file(req, res);
162
+  })
163
+}
164
+
117 165
 
118 166
 app.listen(3000, function () {
119 167
   console.log('Codex Server – listening on port 3000')

+ 2 - 0
package.json

@@ -17,8 +17,10 @@
17 17
   },
18 18
   "dependencies": {
19 19
     "ascii2mathml": "^0.5.4",
20
+    "body-parser": "^1.17.2",
20 21
     "ejs": "^2.5.6",
21 22
     "express": "^4.15.2",
23
+    "lodash": "^4.17.4",
22 24
     "markdown": "^0.5.0",
23 25
     "markdown-it": "^8.3.1",
24 26
     "markdown-it-highlightjs": "^3.0.0",