douxuanling6523 2017-09-16 07:18
浏览 77

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

I'm using tty.js project terminal in web application (https://github.com/chjj/tty.js/), 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();

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

?>
  • 写回答

1条回答 默认 最新

  • doujia2386 2017-09-16 10:10
    关注

    In nodeJS, we don't use files to accept the request, we use routes and path. So you need to make a route for accepting the request made from the frontend.

    In your case it could be done by using the instance returned from var app = tty.createServer, we will add routes and their description to app(instance of server created using tty).

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

    I guess you will want to make a route like POST /folder-tree. Then you will have to do something like:

    // 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 */);
    });
    

    and you will make request from frontend using ajax like:

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题