-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseverse.js
More file actions
138 lines (137 loc) · 3.94 KB
/
severse.js
File metadata and controls
138 lines (137 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let http=require('http');
let fs=require('fs');
let url=require('url');
let sliders=require('./slide')
let read=(cd)=>{
fs.readFile('./books.json','utf8',function (err,data) {
if(err||data.length==0){
cd([]);
}
else {
cd(JSON.parse(data));
}
})
}
let write=(data,cd)=>{
fs.writeFile('./books.json',JSON.stringify(data),cd)
}
http.createServer((req,res)=>{
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.setHeader("X-Powered-By",' 3.2.1')
if(req.method=="OPTIONS") return res.end();/*让options请求快速返回*/
let {pathname,query}=url.parse(req.url,true);
let pagesize=5;
if(pathname==='/sliders'){
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify(sliders));
return;
}
if(pathname==='/hot'){
read(function (books) {
let hot=books.reverse().slice(0,6);
res.setHeader('Content-Type','application/json;charset=utf8');
setTimeout(()=>{
res.end(JSON.stringify(hot));
},1000)
})
return;
}
if(pathname==='/book'){
let i=query.id;
let id=parseInt(i);
switch (req.method){
case 'GET':
if(typeof (i)==='string'){
read(function (books) {
let book= books.find(item=> item.bookId==id);
if(!book) book={};
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify(book));
});
}else {
read(function (books) {
let book=books.reverse();
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify(book));
})
}
break;
case 'POST':
let str='';
req.on('data',chunk=>{
str+=chunk;
});
req.on('end',()=>{
let book=JSON.parse(str);
read(function (books) {
book.bookId=books.length?parseInt(books[books.length-1].bookId)+1:1;
books.push(book);
write(books,function () {
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify(book));
})
})
})
break;
case 'PUT':
if(id){
let str='';
req.on('data',chunk=>{
str+=chunk;
})
req.on('end',()=>{
let book=JSON.parse(str);
read(function (books) {
books= books.map((item)=>{
if(item.bookId==id)
return book;
return item;
})
write(books,function (){
res.end(JSON.stringify(book));
})
})
})
}
break;
case 'DELETE':
read(function (books) {
let book=books.filter(item=>item.bookId!=id);
write(book,function () {
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify({}));
})
})
break;
}
return;
}
if(pathname==='/page'){
let offset=parseInt(query.offset)||0;
read(function (books) {
let result=books.reverse().slice(offset,offset+pagesize);
let hasMore=true;
if(books.length<=offset+pagesize){
hasMore=false;
}
res.setHeader('Content-Type','application/json;charset=utf8');
res.end(JSON.stringify({hasMore,books:result}))
})
return;
}
fs.stat('.'+pathname,function (err,stats) {
if(err){
res.statusCode=404;
res.end('NOT FOUND');
}else {
if(stats.isDirectory()){
let p= require('path').join('.'+pathname,'./index.html');
fs.createReadStream(p).pipe(res);
}else {
fs.createReadStream('.'+pathname).pipe(res);
}
}
});
}).listen(3000);