Skip to content

Commit c22332e

Browse files
committed
first jsTree
0 parents  commit c22332e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+46233
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# dependencies
2+
node_modules
3+
4+
# logs
5+
npm-debug.log
6+
7+
# System
8+
.DS_Store
9+
10+
# Build
11+
build

app.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
3+
const Koa = require('koa');
4+
const render = require('./src/index');
5+
const path = require('path');
6+
const bodyParser=require('koa-bodyparser')
7+
8+
const controller = require('./controller');
9+
10+
11+
const app = new Koa();
12+
13+
14+
15+
app.use(function (ctx, next) { //设置上下文公共信息
16+
ctx.state = ctx.state || {};
17+
ctx.state.now = new Date();
18+
ctx.state.ip = ctx.ip;
19+
ctx.state.version = '2.0.0';
20+
return next();
21+
});
22+
23+
24+
// 第一个middleware是记录URL以及页面执行时间:
25+
26+
app.use(async (ctx, next) => {
27+
console.log(`Process ${ctx.request.method} ${ctx.request.url}...`);
28+
var
29+
start = new Date().getTime(),
30+
execTime;
31+
await next();
32+
execTime = new Date().getTime() - start;
33+
ctx.response.set('X-Response-Time', `${execTime}ms`);
34+
});
35+
// 第二个middleware处理静态文件:
36+
37+
let staticFiles = require('./static-files');
38+
app.use(staticFiles('/static/', __dirname + '/static'));
39+
app.use(staticFiles('/dist/', __dirname + '/dist'));
40+
41+
// 第三个middleware解析POST请求:
42+
43+
app.use(bodyParser());
44+
// 第四个middleware负责给ctx加上render()来使用Nunjucks:
45+
46+
render(app, {
47+
root: path.join(__dirname, 'view') , //挂在render 设置模板配置项
48+
extname: '.html',
49+
});
50+
// 最后一个middleware处理URL路由:
51+
52+
app.use(controller());
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
// app.use(async function (ctx) {
74+
// const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
75+
// await ctx.render('index', { // 渲染content模板
76+
// users
77+
// });
78+
// });
79+
80+
81+
app.listen(3000);
82+
console.log('open http://localhost:3000');
83+
84+
85+
app.on('error', function (err) {
86+
console.log(err.stack);
87+
});

art-template例子/koa-art-template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 3e877a95b8de9ffe88c393a40805112fc915286d

bootcss测试/2.html

Lines changed: 465 additions & 0 deletions
Large diffs are not rendered by default.

bootcss测试/example.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="zh-CN">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
8+
<title>Bootstrap 101 Template</title>
9+
10+
<!-- Bootstrap -->
11+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
12+
13+
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
14+
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
15+
<!--[if lt IE 9]>
16+
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
17+
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
18+
<![endif]-->
19+
</head>
20+
<body>
21+
<h1>你好,世界!</h1>
22+
23+
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
24+
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
25+
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
26+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
27+
</body>
28+
</html>

controller.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs');
2+
3+
function addMapping(router, mapping) {
4+
console.log('addMapping(router, mapping) mapping=');
5+
console.log(mapping);
6+
// addMapping(router, mapping) mapping=
7+
// { 'GET /': [AsyncFunction: fn_index],
8+
// 'POST /signin': [AsyncFunction: fn_signin],
9+
// 'GET /hello/:name': [AsyncFunction: fn_hello] }
10+
// register URL mapping: GET /
11+
// register URL mapping: POST /signin
12+
// register URL mapping: GET /hello/:name
13+
for (var url in mapping) {
14+
if (url.startsWith('GET ')) {
15+
var path = url.substring(4);
16+
router.get(path, mapping[url]); //(path,fn)
17+
console.log(`register URL mapping: GET ${path}`);
18+
} else if (url.startsWith('POST ')) {
19+
var path = url.substring(5);
20+
router.post(path, mapping[url]);
21+
console.log(`register URL mapping: POST ${path}`);
22+
} else {
23+
console.log(`invalid URL: ${url}`);
24+
}
25+
}
26+
}
27+
28+
function addControllers(router) {
29+
var files = fs.readdirSync(__dirname + '/controllers');
30+
var js_files = files.filter((f) => {
31+
return f.endsWith('.js');
32+
});
33+
34+
for (var f of js_files) {
35+
console.log("var f of js_files f->"+f);
36+
37+
console.log(`process controller: ${f}...`);
38+
let mapping = require(__dirname + '/controllers/' + f);
39+
addMapping(router, mapping);
40+
}
41+
}
42+
43+
module.exports = function (dir) {
44+
let
45+
controllers_dir = dir || 'controllers', // 如果不传参数,扫描目录默认为'controllers'
46+
router = require('koa-router')();
47+
addControllers(router, controllers_dir);
48+
return router.routes();
49+
};

controllers/index.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
2+
var fn_index = async (ctx, next) => {
3+
ctx.cookies.set('userinfo',"unknown",{
4+
maxAge:1000*60*60*24
5+
})
6+
const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
7+
await ctx.render('index', { // 渲染content模板
8+
users
9+
});
10+
};
11+
// var fn_index = async (ctx, next) => {
12+
// ctx.response.body = `<h1>Index</h1>
13+
// <form action="/signin" method="post">
14+
// <p>Name: <input name="name" value="koa"></p>
15+
// <p>Password: <input name="password" type="password"></p>
16+
// <p><input type="submit" value="Submit"></p>
17+
// </form>`;
18+
// };
19+
20+
21+
// async (ctx) {
22+
// const users = [{ name: 'Dead Horse' }, { name: 'Jack' }, { name: 'Tom' }];
23+
// await ctx.render('content', { // 渲染content模板
24+
// users
25+
// });
26+
// }
27+
28+
29+
30+
31+
32+
var fn_signin = async (ctx, next) => {
33+
var userinfo=ctx.cookies.get('userinfo')
34+
console.log("userinfo:"+userinfo);
35+
36+
var
37+
name = ctx.request.body.name || '',
38+
password = ctx.request.body.password || '';
39+
console.log(`signin with name: ${name}, password: ${password}`);
40+
if (name === 'autojs@qq.com' && password === '123') {
41+
const user = name
42+
await ctx.render('user', { // 渲染content模板
43+
user
44+
});
45+
} else {
46+
ctx.response.body = `<h1>Login failed!</h1>
47+
<p><a href="/">Try again</a></p>`;
48+
}
49+
};
50+
var fn_getDirInfo = async (ctx, next) => {
51+
console.log('成功进入 fn_getDirInfo ...');
52+
53+
let fs = require('fs');
54+
let join = require('path').join;
55+
let path = require('path');
56+
/**
57+
*
58+
* @param startPath 起始目录文件夹路径
59+
* @returns {Array}
60+
*/
61+
// function findSync(startPath) {
62+
// let result=fs.readdirSync(startPath);
63+
// return result;
64+
// }
65+
[
66+
{ "text" : "Root node", "children" : [
67+
{ "text" : "Child node 1" },
68+
{ "text" : "Child node 2" }
69+
]
70+
},
71+
{ "text" : "Root node", "children" : [
72+
{ "text" : "Child node 1" },
73+
{ "text" : "Child node 2" }
74+
]
75+
}
76+
]
77+
function findSync(startPath) {
78+
let result=[];
79+
function finder(path) {
80+
let files=fs.readdirSync(path);
81+
files.forEach((val,index) => {
82+
83+
84+
let fPath=join(path,val);
85+
let stats=fs.statSync(fPath);
86+
if(stats.isDirectory()) {
87+
let 子文件数组=fs.readdirSync(fPath);
88+
for(let i=0;i<子文件数组.length;i++){
89+
子文件数组[i]={
90+
"text":子文件数组[i],
91+
"icon":"https://www.jstree.com/tree.png"
92+
}
93+
}
94+
95+
result.push({
96+
"text":val,
97+
"children":子文件数组
98+
});
99+
子文件数组=fs.readdirSync(fPath);
100+
for(let i=0;i<子文件数组.length;i++){
101+
let inFPath=fPath+"/"+子文件数组[i]
102+
let stats=fs.statSync(inFPath);
103+
if(stats.isDirectory()) {
104+
finder(inFPath)
105+
}
106+
}
107+
};
108+
if(stats.isFile()) {
109+
110+
result.push({
111+
"text":val,
112+
"icon":"https://www.jstree.com/tree.png"
113+
});
114+
};
115+
});
116+
117+
}
118+
finder(startPath);
119+
return result;
120+
}
121+
var dirInfo=null
122+
var root=""
123+
// path.join(__dirname, "./test.txt")
124+
var dir = ctx.request.body.dir || "../projectList"
125+
console.log('dir='+dir)
126+
if(dir == "../projectList"){
127+
//显示根目录的所有文件夹
128+
dir=path.join(__dirname, dir)
129+
dirInfo=findSync(dir);
130+
}else{
131+
//显示指定目录下的文件
132+
dir=path.join(__dirname, "../projectList/"+dir)
133+
dirInfo=findSync(dir);
134+
}
135+
console.log("dirInfo=")
136+
console.log(dirInfo)
137+
//数组
138+
if (dirInfo) {
139+
ctx.response.body = dirInfo;
140+
} else {
141+
ctx.response.body = `<h1>获取文件夹${dir}目录信息失败</h1>`;
142+
}
143+
// [
144+
// { "text" : "Root node", "children" : [
145+
// { "text" : "Child node 1" },
146+
// { "text" : "Child node 2" }
147+
// ]
148+
// },
149+
// { "text" : "Root node", "children" : [
150+
// { "text" : "Child node 1" },
151+
// { "text" : "Child node 2" }
152+
// ]
153+
// }
154+
// ]
155+
// ["script06.js","test","test2"]
156+
// ["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"]
157+
};
158+
var fn_hello = async (ctx, next) => {
159+
var name = ctx.params.name;
160+
ctx.response.body = `<h1>Hello, ${name}!</h1>`;
161+
};
162+
163+
164+
module.exports = {
165+
'GET /': fn_index,
166+
'POST /signin': fn_signin,
167+
'POST /getDirInfo': fn_getDirInfo,
168+
'GET /hello/:name': fn_hello
169+
};

controllers/login.js

Whitespace-only changes.

controllers/other.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
async (ctx, next) => {
2+
ctx.render('index.html', {
3+
title: 'Welcome'
4+
});
5+
}
6+
7+
async (ctx, next) => {
8+
var
9+
email = ctx.request.body.email || '',
10+
password = ctx.request.body.password || '';
11+
if (email === 'admin@example.com' && password === '123456') {
12+
// 登录成功:
13+
ctx.render('signin-ok.html', {
14+
title: 'Sign In OK',
15+
name: 'Mr Node'
16+
});
17+
} else {
18+
// 登录失败:
19+
ctx.render('signin-failed.html', {
20+
title: 'Sign In Failed'
21+
});
22+
}
23+
}

controllers/users.js

Whitespace-only changes.

0 commit comments

Comments
 (0)