samedi 16 septembre 2017

Post PHP script using Node.js & JQuery

I'm using tty.js project terminal in web application (http://ift.tt/1iLW3IN), and I wanted to add jquery treeview to the home page and display root folders. the jquery treeview I tested it on apache works fine but when I use it on node.js web app i get this error : I can download the htps://localhost:10443/Foldertree.php but but cant POST data error 404 not found.

Error 404 Not Found

Here is the scenario: (Note I'm new to node.js).

app.server SETTING :

var tty = require('tty.js');
var app = tty.createServer({
  "users": {
    "user": "password"
  },
  "https": {
    "key": "./skey.key",
    "cert": "./scert.crt"
  },
  "port": 10443,
  "hostname": "localhost",
  "shell": "sh",
  "static": "./static",
  "debug": true,
  "term": {
    "termName": "xterm",
    "geometry": [80, 24],
    "scrollback": 1000,
    "visualBell": true,
    "popOnBell": true,
    "cursorBlink": true,
    "screenKeys": true,
    "colors": [
      "#2e3436",
      "#cc0000",
      "#4e9a06",
      "#c4a000",
      "#3465a4",
      "#75507b",
      "#06989a",
      "#d3d7cf",
      "#555753",
      "#ef2929",
      "#8ae234",
      "#fce94f",
      "#729fcf",
      "#ad7fa8",
      "#34e2e2",
      "#eeeeec"
    ]
  } 
});
app.listen();

/http://ift.tt/2fcxb54 :

<!doctype html>
<title>TEST01</title>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="sweetalert2.css">
<link rel="stylesheet" href="css/filetree.css" type="text/css" >
<script type="text/javascript" src="http://ift.tt/1rJStC0"></script>
<script type="text/javascript" src="sweetalert2.js"></script>
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript" src="http://ift.tt/1aeIZU4"></script>
<script type="text/javascript" src="term.js" ></script>
<script type="text/javascript" src="options.js"></script>
<script type="text/javascript" src="tty.js"></script>
<script type="text/javascript" src="jqueryFileTree.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready( function() {

    $( '#container' ).html( '<ul class="filetree start"><li class="wait">' + 'Generating Tree...' + '<li></ul>' );

    getfilelist( $('#container') , 'Sample' );

    function getfilelist( cont, root ) {

        $( cont ).addClass( 'wait' );


        $.post('Foldertree.php', { dir: root }, function( data ) {
            $( cont ).find( '.start' ).html( '' );
            $( cont ).removeClass( 'wait' ).append( data );

            if( 'Sample' == root ) 
                $( cont ).find('UL:hidden').show();
            else 
                $( cont ).find('UL:hidden').slideDown({ duration: 500, easing: null });

        });
    }

    $( '#container' ).on('click', 'LI A', function() {
        var entry = $(this).parent();

        if( entry.hasClass('folder') ) {
            if( entry.hasClass('collapsed') ) {

                entry.find('UL').remove();
                getfilelist( entry, escape( $(this).attr('rel') ));
                entry.removeClass('collapsed').addClass('expanded');
            }
            else {

                entry.find('UL').slideUp({ duration: 500, easing: null });
                entry.removeClass('expanded').addClass('collapsed');
            }
        } else {
            $( '#selected_file' ).text( "File:  " + $(this).attr( 'rel' ));
        }
    return false;
    });

});
</script>

</head>
<h1>TEST01</h1>
<body>
<button class="pure-button pure-button-active" onclick="sweet();">ScreenShot</button>
<button class="pure-button pure-button-active" id="open">Terminal</button>
<button class="pure-button pure-button-active" id="lights">Lights</button>
<div id="container"> </div>
<div id="selected_file"></div>      
</div>
</body>
</html>

PHP SCRIPT /http://ift.tt/2xpssUb :

<?php
class treeview {

    private $files;
    private $folder;

    function __construct( $path ) {

        $files = array();   

        if( file_exists( $path)) {
            if( $path[ strlen( $path ) - 1 ] ==  '/' )
                $this->folder = $path;
            else
                $this->folder = $path . '/';

            $this->dir = opendir( $path );
            while(( $file = readdir( $this->dir ) ) != false )
                $this->files[] = $file;
            closedir( $this->dir );
        }
    }

    function create_tree( ) {

        if( count( $this->files ) > 2 ) { /* First 2 entries are . and ..  -skip them */
            natcasesort( $this->files );
            $list = '<ul class="filetree" style="display: none;">';
            // Group folders first
            foreach( $this->files as $file ) {
                if( file_exists( $this->folder . $file ) && $file != '.' && $file != '..' && is_dir( $this->folder . $file )) {
                    $list .= '<li class="folder collapsed"><a href="#" rel="' . htmlentities( $this->folder . $file ) . '/">' . htmlentities( $file ) . '</a></li>';
                }
            }
            // Group all files
            foreach( $this->files as $file ) {
                if( file_exists( $this->folder . $file ) && $file != '.' && $file != '..' && !is_dir( $this->folder . $file )) {
                    $ext = preg_replace('/^.*\./', '', $file);
                    $list .= '<li class="file ext_' . $ext . '"><a href="#" rel="' . htmlentities( $this->folder . $file ) . '">' . htmlentities( $file ) . '</a></li>';
                }
            }
            $list .= '</ul>';   
            return $list;
        }
    }
}

$path = urldecode( $_REQUEST['dir'] );
$tree = new treeview( $path );
echo $tree->create_tree();

?>

Aucun commentaire:

Enregistrer un commentaire