繁体   English   中英

使用Node.js和JQuery发布PHP脚本

[英]Post PHP script using Node.js & JQuery

我在Web应用程序( https://github.com/chjj/tty.js/ )中使用tty.js项目终端,我想将jquery treeview添加到主页并显示根文件夹。 我在apache上测试过的jquery treeview工作正常,但是当我在node.js Web应用程序上使用它时,出现此错误:我可以下载htps:// localhost:10443 / Foldertree.php,但找不到POST数据错误404 。

未找到错误404

这是场景:(注意,我是node.js的新手)。

app.server设置:

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();

/tty.js/static/index.html:

<!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="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="sweetalert2.js"></script>
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript" src="socket.io/socket.io.js"></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脚本/tty.js/static/Foldertree.php:

<?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();

?>

在nodeJS中,我们不使用文件来接受请求,而是使用路由和路径。 因此,您需要制定一条路线来接受前端发出的请求。

在您的情况下,可以使用从var app = tty.createServer返回的实例来完成,我们将向app(instance of server created using tty)添加路由及其描述。

// sample route
app.get('/foo', function(req, res, next) {
  res.send('bar');
});

我猜你会想要像POST /folder-tree这样的路由。 然后,您将必须执行以下操作:

// sample route
app.post('/folder-tree', function(req, res, next) {

    // do your file processing using package named - fs

    // sending the response
    return res.status(200).json(/* finalFolderTree */);
});

并且您将使用ajax从前端发出请求,例如:

$.post('/folder-tree', { dir: root }, function( data ) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM