Node.js 相近实战(四) 之图书管理系统(图书查询)
Node.js 切近实战(四) 之图书管理系统(图书查询)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#book_retrieve(ng-controller='bookRetrieveCtrl') .row
.col-md-4
label Book Name:
span.k-textbox.k-space-right(style='margin-left:5px;width:70%')
input(type='text' ng-keydown='getBook($event)' ng-model='Search.BookName')
a.k-icon.k-i-search(href="javascript:void(0)" ng-click='getBook()')
.col-md-4
label ISBN:
span.k-textbox.k-space-right(style='margin-left:5px;width:70%')
input(type='text' ng-keydown='getBook($event)' ng-model='Search.ISBN')
a.k-icon.k-i-search(href='javascript:void(0)' ng-click='getBook()')
hr.panel-line
kendo-grid(options='bookGridOptions' k-data-source='BookList')
div(kendo-window='modals' k-width='500' k-modal='true' k-visible='false' k-title='"Book Image Upload"' k-on-close='uploadClose()')
.row.row-margin
.col-md-11
kendo-upload(type='file' name='files' k-multiple='false' k-success='onUploadSuccess' k-upload='upload' k-error='onUploadError' k-async='{saveUrl:"/book/upload", autoUpload: false}')
block scripts script(type='text/javascript' src='/javascripts/local/book/bookRetrieve.js')
|
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
|
var appModule = angular.module( 'bookRetrieveModule' , [ "kendo.directives" ]);
appModule.config( function ($locationProvider) {
$locationProvider.html5Mode( true );
}); appModule.controller( 'bookRetrieveCtrl' , function ($scope, $http, $location) {
Messenger.options = {
extraClasses: 'messenger-fixed messenger-on-top messenger-on-center' ,
theme: 'flat'
}
$scope.showMsg = function (type, msg) {
Messenger().post({
message: msg,
type: type,
hideAfter: 2,
showCloseButton: true
});
}
$scope.BookId = '' ;
$scope.Search = {};
$scope.uploadWindow = {
open: function () {
$scope.modals.center().open();
}
};
$scope.bookGridOptions = {
height: 700,
sortable: true ,
pageable: {
refresh: true ,
pageSizes: [10, 20, 50, 100],
buttonCount: 5
},
resizable: true ,
selectable: "single" ,
columns: [{
template: "<div class='center-align-text'>" +
"<a href='/book/image/#=Image#'><img src='/book/image/#=Image#' class='img-inline'/></a></div>" ,
field: "Image" ,
title: "Image" ,
width: 130
}, {
field: "Title" ,
title: "Title"
}, {
field: "Author" ,
title: "Author"
}, {
field: "Price" ,
title: "Price" ,
width: 60,
}, {
field: "ISBN" ,
title: "ISBN"
}, {
field: "Press" ,
title: "Press"
}, {
command: [
{
name: "Upload" ,
text: "Upload" ,
imageClass: 'k-icon k-insertImage' ,
click: function (e) {
var dataItem = this .dataItem($(e.currentTarget).closest( "tr" ));
$scope.BookId = dataItem._id;
$scope.uploadWindow.open();
}
}],
width: 150,
}], dataBound: function (rowBoundEvent) {
$( "div.center-align-text a" ).popImage();
}
}
$scope.getBook = function (event) {
if (!event || event.keyCode == 13) {
$scope.BookList = new kendo.data.DataSource({
"pageSize" : 15,
"serverPaging" : true ,
transport: {
read: function (e) {
var url = '/book?pageIndex=' + (e.data.page-1) + '&pageSize=' + e.data.pageSize;
if ($scope.Search.BookName) {
url += '?bookName=' + $scope.Search.BookName
}
if ($scope.Search.ISBN) {
url += '&ISBN=' + $scope.Search.ISBN;
}
$http.get(url).success( function (data) {
e.success(data);
});
}
},
schema: {
data: function (dataset) {
return dataset.books || [];
},
total: function (dataset) {
return dataset.totalCount || 0;
}
}
});
}
}
$scope.onUploadError = function (error) {
$scope.showMsg( 'error' , angular.fromJson(error.XMLHttpRequest.responseText).error);
}
$scope.onUploadSuccess = function (e) {
var response = e.response;
if (response.isSuc) {
$scope.showMsg( 'success' , 'Upload completed successfully!' );
}
else {
$scope.showMsg( 'error' , response.msg);
}
}
$scope.upload = function (e) {
e.sender.options.async.saveUrl = "/book/upload?bookId=" + $scope.BookId;
}
}); angular.element( '#book_retrieve' ).data( '$injector' , '' );
angular.bootstrap(angular.element( '#book_retrieve' ), [ 'bookRetrieveModule' ]);
|
1
|
router.get( '/book' , bookRoutes.getBookList);
|
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
|
exports.getBookList = function (req, res) {
var bookName = req.query.bookName;
var ISBN = req.query.ISBN;
var pageIndex = req.query.pageIndex;
var pageSize = req.query.pageSize;
var query = bookModel.find({});
if (bookName) {
query = query.where({ 'Title' : { "$regex" : bookName, "$options" : "i" } });
}
if (ISBN) {
var regexp = new RegExp( "^" + ISBN);
query = query.where({ 'ISBN' : regexp });
}
query.count().exec( function (error, count) {
query.limit(pageSize)
.skip(pageIndex * pageSize)
.sort( '-PressDate' )
.exec( 'find' , function (error, doc) {
res.json({ books: doc, totalCount: count });
});
});
} |
1
|
router.get( '/book/image/:id' , bookRoutes.getBookImageById);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
var fs = require( 'fs' );
var Grid = require( 'gridfs-stream' );
var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
exports.getBookImageById = function (req, res) {
gfs.exist({ _id: req.params.id }, function (error, exists) {
if (!exists) {
var rstream = fs.createReadStream( './public/images/noimage.jpg' );
rstream.pipe(res);
}
else {
var readstream = gfs.createReadStream({
_id: new mongoose.Types.ObjectId(req.params.id)
});
readstream.on( 'error' , function (err) {
console.log(err);
res.send(500, err);
});
readstream.pipe(res);
}
})
} |
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
|
var bookSchemas = require( '../model/bookinfo.js' );
var bookMsgRes = require( '../framework/message/book_msg.js' );
var validator = require( 'validator' );
var fs = require( 'fs' );
var Busboy = require( 'busboy' );
var mongoose = require( 'mongoose' );
var Grid = require( 'gridfs-stream' );
var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
exports.fileupload = function (req, res, next) {
if (!/multipart\/form-data/i.test(req.headers[ 'content-type' ])) {
return res.status(500).end( 'Wrong content type' );
}
var busboy = new Busboy({
headers: req.headers, limits: {
files: 1,
fileSize: 1024 * 1024 * 2
}
});
busboy.on( 'file' , function (fieldname, file, filename, encoding, mimetype) {
if (mimetype != 'image/jpeg' && mimetype != 'image/png' && mimetype != 'image/bmp' ) {
res.status(403).json({ isSuc: false , error: 'Can\'t upload(' + filename + ') if file\'s type is not image(jpg,png,bmp)!' });
return ;
}
var fileId = new mongoose.Types.ObjectId();
var readstream = gfs.createWriteStream({
_id: fileId,
mode: 'w' ,
content_type: mimetype,
filename: filename,
metadata: {
uploaddate: Date.now()
}
});
file.pipe(readstream);
bookModel.findByIdAndUpdate(req.query.bookId, { $set: { Image: fileId } }, function (error, doc) { });
});
busboy.on( 'finish' , function () {
res.json({ isSuc: true });
});
busboy.on( 'error' , function (err) {
res.status(500).end({ isSuc: false , msg: err });
});
req.pipe(busboy);
}; |