Skip to content

Commit aaeb96b

Browse files
committed
增加在gulpfile中读取json文件的代码
1 parent bb2ca7c commit aaeb96b

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

2-gulp/2.mock数据.md

+53-43
Original file line numberDiff line numberDiff line change
@@ -40,62 +40,72 @@
4040

4141
```javascript
4242

43-
var gulp = require('gulp');
43+
var gulp = require('gulp')
4444

45-
var connect = require('gulp-connect');
45+
var connect = require('gulp-connect')
4646

47-
var webserver = require('gulp-webserver');
47+
var webserver = require('gulp-webserver')
4848

4949
var urlTool = require('url')
5050

5151
var qs = require('qs')
5252

53+
var fs = require('fs')
5354

54-
var dataBase = {
55-
users:[
56-
{
57-
name:'zhangsan',
58-
password:123456
59-
},
60-
{
61-
name:'lisi',
62-
password:123456
63-
},
64-
],
65-
goodslist:[
66-
{
67-
name:'page1',
68-
data:[
69-
{
70-
name:'商品1',
71-
price:.1
72-
},
73-
{
74-
name:'商品2',
75-
price:.2
76-
}
77-
]
78-
},
79-
{
80-
name:'page2',
81-
data:[
82-
{
83-
name:'商品3',
84-
price:.3
85-
},
86-
{
87-
name:'商品4',
88-
price:.4
89-
}
90-
]
91-
}
92-
],
93-
home:'<html><meta charset="utf-8"; /> <style> div{background:red}</style> <div>这是首页</div></html>'
55+
var hasDataBase = fs.existsSync('./dataBase.json')
56+
57+
var dataBase = {}
9458

59+
if(hasDataBase){
60+
dataBase = JSON.parse(fs.readFileSync('./dataBase.json').toString())
61+
}else{
62+
dataBase = {
63+
users:[
64+
{
65+
name:'zhangsan',
66+
password:123456
67+
},
68+
{
69+
name:'lisi',
70+
password:123456
71+
},
72+
],
73+
goodslist:[
74+
{
75+
name:'page1',
76+
data:[
77+
{
78+
name:'商品1',
79+
price:.1
80+
},
81+
{
82+
name:'商品2',
83+
price:.2
84+
}
85+
]
86+
},
87+
{
88+
name:'page2',
89+
data:[
90+
{
91+
name:'商品3',
92+
price:.3
93+
},
94+
{
95+
name:'商品4',
96+
price:.4
97+
}
98+
]
99+
}
100+
],
101+
home:'<html><meta charset="utf-8"; /> <style> div{background:red}</style> <div>这是首页</div></html>'
102+
}
95103
}
96104

97105

98106

107+
108+
99109
function login(userName,password){
100110

101111
var exist = false;

0 commit comments

Comments
 (0)