Skip to content

Commit

Permalink
first jsTree
Browse files Browse the repository at this point in the history
  • Loading branch information
snailuncle committed Dec 7, 2018
0 parents commit c22332e
Show file tree
Hide file tree
Showing 81 changed files with 46,233 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# dependencies
node_modules

# logs
npm-debug.log

# System
.DS_Store

# Build
build
87 changes: 87 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@


const Koa = require('koa');
const render = require('./src/index');
const path = require('path');
const bodyParser=require('koa-bodyparser')

const controller = require('./controller');


const app = new Koa();



app.use(function (ctx, next) { //设置上下文公共信息
ctx.state = ctx.state || {};
ctx.state.now = new Date();
ctx.state.ip = ctx.ip;
ctx.state.version = '2.0.0';
return next();
});


// 第一个middleware是记录URL以及页面执行时间:

app.use(async (ctx, next) => {
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
var
start = new Date().getTime(),
execTime;
await next();
execTime = new Date().getTime() - start;
ctx.response.set('X-Response-Time', `${execTime}ms`);
});
// 第二个middleware处理静态文件:

let staticFiles = require('./static-files');
app.use(staticFiles('/static/', __dirname + '/static'));
app.use(staticFiles('/dist/', __dirname + '/dist'));

// 第三个middleware解析POST请求:

app.use(bodyParser());
// 第四个middleware负责给ctx加上render()来使用Nunjucks:

render(app, {
root: path.join(__dirname, 'view') , //挂在render 设置模板配置项
extname: '.html',
});
// 最后一个middleware处理URL路由:

app.use(controller());




















// app.use(async function (ctx) {
// const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
// await ctx.render('index', { // 渲染content模板
// users
// });
// });


app.listen(3000);
console.log('open http://localhost:3000');


app.on('error', function (err) {
console.log(err.stack);
});
1 change: 1 addition & 0 deletions art-template例子/koa-art-template
Submodule koa-art-template added at 3e877a
465 changes: 465 additions & 0 deletions bootcss测试/2.html

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions bootcss测试/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>你好,世界!</h1>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
49 changes: 49 additions & 0 deletions controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require('fs');

function addMapping(router, mapping) {
console.log('addMapping(router, mapping) mapping=');
console.log(mapping);
// addMapping(router, mapping) mapping=
// { 'GET /': [AsyncFunction: fn_index],
// 'POST /signin': [AsyncFunction: fn_signin],
// 'GET /hello/:name': [AsyncFunction: fn_hello] }
// register URL mapping: GET /
// register URL mapping: POST /signin
// register URL mapping: GET /hello/:name
for (var url in mapping) {
if (url.startsWith('GET ')) {
var path = url.substring(4);
router.get(path, mapping[url]); //(path,fn)
console.log(`register URL mapping: GET ${path}`);
} else if (url.startsWith('POST ')) {
var path = url.substring(5);
router.post(path, mapping[url]);
console.log(`register URL mapping: POST ${path}`);
} else {
console.log(`invalid URL: ${url}`);
}
}
}

function addControllers(router) {
var files = fs.readdirSync(__dirname + '/controllers');
var js_files = files.filter((f) => {
return f.endsWith('.js');
});

for (var f of js_files) {
console.log("var f of js_files f->"+f);

console.log(`process controller: ${f}...`);
let mapping = require(__dirname + '/controllers/' + f);
addMapping(router, mapping);
}
}

module.exports = function (dir) {
let
controllers_dir = dir || 'controllers', // 如果不传参数,扫描目录默认为'controllers'
router = require('koa-router')();
addControllers(router, controllers_dir);
return router.routes();
};
169 changes: 169 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@

var fn_index = async (ctx, next) => {
ctx.cookies.set('userinfo',"unknown",{
maxAge:1000*60*60*24
})
const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
await ctx.render('index', { // 渲染content模板
users
});
};
// var fn_index = async (ctx, next) => {
// ctx.response.body = `<h1>Index</h1>
// <form action="/signin" method="post">
// <p>Name: <input name="name" value="koa"></p>
// <p>Password: <input name="password" type="password"></p>
// <p><input type="submit" value="Submit"></p>
// </form>`;
// };


// async (ctx) {
// const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
// await ctx.render('content', { // 渲染content模板
// users
// });
// }





var fn_signin = async (ctx, next) => {
var userinfo=ctx.cookies.get('userinfo')
console.log("userinfo:"+userinfo);

var
name = ctx.request.body.name || '',
password = ctx.request.body.password || '';
console.log(`signin with name: ${name}, password: ${password}`);
if (name === 'autojs@qq.com' && password === '123') {
const user = name
await ctx.render('user', { // 渲染content模板
user
});
} else {
ctx.response.body = `<h1>Login failed!</h1>
<p><a href="/">Try again</a></p>`;
}
};
var fn_getDirInfo = async (ctx, next) => {
console.log('成功进入 fn_getDirInfo ...');

let fs = require('fs');
let join = require('path').join;
let path = require('path');
/**
*
* @param startPath 起始目录文件夹路径
* @returns {Array}
*/
// function findSync(startPath) {
// let result=fs.readdirSync(startPath);
// return result;
// }
[
{ "text" : "Root node", "children" : [
{ "text" : "Child node 1" },
{ "text" : "Child node 2" }
]
},
{ "text" : "Root node", "children" : [
{ "text" : "Child node 1" },
{ "text" : "Child node 2" }
]
}
]
function findSync(startPath) {
let result=[];
function finder(path) {
let files=fs.readdirSync(path);
files.forEach((val,index) => {


let fPath=join(path,val);
let stats=fs.statSync(fPath);
if(stats.isDirectory()) {
let 子文件数组=fs.readdirSync(fPath);
for(let i=0;i<子文件数组.length;i++){
子文件数组[i]={
"text":子文件数组[i],
"icon":"https://www.jstree.com/tree.png"
}
}

result.push({
"text":val,
"children":子文件数组
});
子文件数组=fs.readdirSync(fPath);
for(let i=0;i<子文件数组.length;i++){
let inFPath=fPath+"/"+子文件数组[i]
let stats=fs.statSync(inFPath);
if(stats.isDirectory()) {
finder(inFPath)
}
}
};
if(stats.isFile()) {

result.push({
"text":val,
"icon":"https://www.jstree.com/tree.png"
});
};
});

}
finder(startPath);
return result;
}
var dirInfo=null
var root=""
// path.join(__dirname, "./test.txt")
var dir = ctx.request.body.dir || "../projectList"
console.log('dir='+dir)
if(dir == "../projectList"){
//显示根目录的所有文件夹
dir=path.join(__dirname, dir)
dirInfo=findSync(dir);
}else{
//显示指定目录下的文件
dir=path.join(__dirname, "../projectList/"+dir)
dirInfo=findSync(dir);
}
console.log("dirInfo=")
console.log(dirInfo)
//数组
if (dirInfo) {
ctx.response.body = dirInfo;
} else {
ctx.response.body = `<h1>获取文件夹${dir}目录信息失败</h1>`;
}
// [
// { "text" : "Root node", "children" : [
// { "text" : "Child node 1" },
// { "text" : "Child node 2" }
// ]
// },
// { "text" : "Root node", "children" : [
// { "text" : "Child node 1" },
// { "text" : "Child node 2" }
// ]
// }
// ]
// ["script06.js","test","test2"]
// ["F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\script06.js","F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\test\\index.js","F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\test\\script01.js","F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\test\\script02.js","F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\test2\\script04.js","F:\\koa2\\666666666666666666666666666666666666666666666666666666666666\\douban-trailer-mooc\\koa开始服务器编写\\projectList\\test2\\script05.js"]
};
var fn_hello = async (ctx, next) => {
var name = ctx.params.name;
ctx.response.body = `<h1>Hello, ${name}!</h1>`;
};


module.exports = {
'GET /': fn_index,
'POST /signin': fn_signin,
'POST /getDirInfo': fn_getDirInfo,
'GET /hello/:name': fn_hello
};
Empty file added controllers/login.js
Empty file.
23 changes: 23 additions & 0 deletions controllers/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
async (ctx, next) => {
ctx.render('index.html', {
title: 'Welcome'
});
}

async (ctx, next) => {
var
email = ctx.request.body.email || '',
password = ctx.request.body.password || '';
if (email === 'admin@example.com' && password === '123456') {
// 登录成功:
ctx.render('signin-ok.html', {
title: 'Sign In OK',
name: 'Mr Node'
});
} else {
// 登录失败:
ctx.render('signin-failed.html', {
title: 'Sign In Failed'
});
}
}
Empty file added controllers/users.js
Empty file.
Loading

0 comments on commit c22332e

Please sign in to comment.