module.exports = function(app, logger, event) {
    logger.info('module main');
    var assert = require('assert');
    app.get('/test', function(req, res) {
        res.send('Hello this is the Openfpgaduino!');
    });

    app.post('/', function(req, res) {
        res.statusCode = 302;
        res.setHeader("Location", "index.html");
        res.end();
    });

    var fs = require('fs');
    var path = require('path');

    app.get('/list', function(req, res) {
        var filelist = [];
        fs.readdirSync(__dirname + "/").forEach(function(filename) {
            if (!/\.js$/.test(filename)) {
                return;
            }
            filelist.push(filename);

        });
        res.json({
            script: filelist
        });
    });

    app.get('/get/:filename', function(req, res) {
        var filename = req.params.filename;
        var code = fs.readFileSync(__dirname + "/" + filename, "utf8");
        res.send(code);
    });

    app.post('/log', function(req, res) {
        var length = parseInt(req.body.length);
        var position = parseInt(req.body.position);
        var fd = fs.openSync("server.log", 'r');
        var buffer = new Buffer(length);
        length = fs.readSync(fd, buffer, 0, length, position);
        res.json({
            log: buffer.toString(),
            length: length
        });
    });

    app.post('/install', function(req, res) {
        var filename = req.body.filename;
        var code = req.body.code;
        fs.writeFileSync(__dirname + "/" + filename, code);
        res.json({
            message: 'app installed'
        });
    });

    app.post('/load', function(req, res) {
        var filename = req.body.filename;
        event.emit('load', filename);
        res.json({
            message: 'app load'
        });
    });

    app.del('/:filename', function(req, res) {
        var filename = req.params.filename;
        var code = fs.unlinkSync(__dirname + "/" + filename);
        res.send(code);
    });
}
@@ -37,16 +37,20 @@
 r, event
+, fs
 ) %7B%0A    
@@ -368,36 +368,8 @@
 );%0A%0A
-    var fs = require('fs');%0A
     
@@ -484,17 +484,18 @@
 .readdir
-S
+As
 ync(__di
@@ -506,16 +506,72 @@
 e + %22/%22)
+%0A            .then(function(list) %7B%0A                list
 .forEach
@@ -604,16 +604,24 @@
         
+ 
+       
 if (!/%5C.
@@ -639,24 +639,32 @@
 ilename)) %7B%0A
+        
             
@@ -691,10 +691,25 @@
     
-%7D%0A
+        %7D%0A       
     
@@ -716,16 +716,17 @@
         
+ 
 filelist
@@ -742,25 +742,30 @@
 ename);%0A
-%0A
+      
         
 %7D);%0A    
@@ -760,20 +760,25 @@
     
-%7D);%0A
+  %7D)%0A    
         
 res.
@@ -765,32 +765,36 @@
  %7D)%0A            
+    
 res.json(%7B%0A     
@@ -780,32 +780,40 @@
      res.json(%7B%0A
+        
             scri
@@ -825,35 +825,190 @@
 ilelist%0A        
+   
+     %7D);%0A            %7D)%0A            .catch(function(error) %7B%0A                res.json(%7B%0A                    error: error%0A                %7D);%0A            
 %7D)
-;
 %0A    %7D);%0A%0A    ap
@@ -1099,35 +1099,24 @@
 ame;%0A       
- var code =
  fs.readFile
@@ -1111,25 +1111,26 @@
  fs.readFile
-S
+As
 ync(__dirnam
@@ -1156,26 +1156,67 @@
  %22utf8%22)
-;
 %0A
+            .then(function(code) %7B%0A       
         
 res.send
@@ -1199,32 +1199,33 @@
 %0A               
+ 
 res.send(code);%0A
@@ -1224,24 +1224,171 @@
 (code);%0A
+            %7D)%0A            .catch(function(error) %7B%0A                res.json(%7B%0A                    error: error%0A                %7D);%0A            %7D)%0A
     %7D);%0A
 %0A    app
@@ -1371,32 +1371,36 @@
      %7D)%0A    %7D);%0A
+    
 %0A    app.post('/
@@ -1537,25 +1537,16 @@
     
- var fd =
  fs.open
 Sync
@@ -1541,17 +1541,18 @@
  fs.open
-S
+As
 ync(%22ser
@@ -1565,18 +1565,58 @@
 g%22, 'r')
-;
 %0A
+            .then(function(fd) %7B%0A        
         
@@ -1660,24 +1660,30 @@
     
-length =
+        return
  fs.read
 Sync
@@ -1678,17 +1678,18 @@
  fs.read
-S
+As
 ync(fd, 
@@ -1708,34 +1708,100 @@
 ength, position)
-;
 %0A
+            %7D)%0A            .spread(function(bytes, data) %7B%0A        
         res.json
@@ -1819,19 +1819,25 @@
     
+  
-log: buffer
+      log: data
 .toS
@@ -1861,16 +1861,24 @@
     
+    
+    
 length: 
 leng
@@ -1877,26 +1877,181 @@
 th: 
-length%0A        %7D);
+bytes%0A                %7D);%0A            %7D)%0A            .catch(function(error) %7B%0A                res.json(%7B%0A                    error: error%0A                %7D);%0A            %7D)%0A
 %0A   

refine the fs operation