Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Backend quản lý sinh viên với NodeJS
Backend quản lý sinh viên với NodeJS
NodeJS
Chạy javascript trên server-side
Mongodb
- NoSql
- Sử dụng cấu trúc key-value
Json
javascript object notation
npm
Node package manager
Express
NodeJS web application framework
Overall
Lập trình NodeJS với Express framework tạo server với các API lưu dữ liệu vào mongodb.
Có các api lấy thông tin sinh viên, tìm sinh viên, thêm sinh viên, cập nhật thông tin sinh viên và xóa thông tin sinh viên.
Github: https://github.com/HungNguyenUET/student-management
Create project
npm init
File index.js
var express = require('express'); //=> NodeJS Web application framework
var app= express();
var MongoClient = require('mongodb').MongoClient; //=> Connect mongodb
var mongoIO = require('./controller/MongoIO'); //=> MongoIO controller
var bodyParser = require('body-parser'); //=> Json
var url = "mongodb://localhost:27018/student_db"; //=> Mongo url port 27018 database student_db
app.use(bodyParser.json());
File MongoIO.js
// data is sudent info with Json
var insertStudent = function (db, callback, data) {
db.collection('student').insertOne(data, function (err, result) {
callback() //=> None bocking
})
};
// data is array
var getAllStudent = function (db, callback, data) {
var cursor = db.collection('student').find();
cursor.each(function (err, doc) {
if (doc != null){
data.push(doc) //=> Push student info into array
} else {
callback()
}
})
};
var findStudent = function (db, callback, filter, data) {
var cursor = db.collection('student').find({"name" : filter});
cursor.each(function (err, doc) {
if(doc != null){
data.push(doc);
}else {
callback()
}
})
};
var updateStudent = function (db, callback, filter, data) {
var cursor = db.collection('student').updateOne(
{"id" : filter}, //=> Filter
{
$set : {"name" : data}, //=> Update
$currentDate : {"lastModifited" : true}
}, function (err, results) {
callback()
}
)
};
var deleteStudent = function (db, callback, filter) {
db.collection('student').deleteOne(
{"id" : filter},
function (err, results) {
callback();
}
)
};
//Public
exports.insertStudent = insertStudent;
exports.getAllStudent = getAllStudent;
exports.findStudent = findStudent;
exports.updateStudent = updateStudent;
exports.deleteStudent = deleteStudent;
File index.js
//API
//Get all student
app.get('/all', function (req, res) {
var data = [];
var dataJson; //data response
MongoClient.connect(url, function (err, db) {
mongoIO.getAllStudent(db, function () {
//object to json
dataJson = JSON.stringify({
data : data
});
db.close();
res.send(dataJson); //=> response body
res.end();
}, data)
});
});
//Get all student with name filter
app.get('/search', function (req, res) {
console.log("Search student with name");
var data = [];
var dataJson;
var filter = req.param('name');
MongoClient.connect(url, function (err, db) {
mongoIO.findStudent(db, function () {
// Callback
dataJson = JSON.stringify({
data : data
});
db.close();
res.send(dataJson);
res.end();
}, filter, data);
db.close();
})
});
//insert student
app.post('/add', function (req, res) {
console.log("Add student info");
//insert into mongo database
var data = req.body;
MongoClient.connect(url, function (err, db) {
mongoIO.insertStudent(db, function () {
db.close()
}, data)
});
res.send(data);
res.end();
});
//update student info
app.put('/update', function (req, res) {
console.log("Update student info");
var filter = req.param("id");
var data = req.param("name");
MongoClient.connect(url, function (err, db) {
mongoIO.updateStudent(db, function () {
db.close();
res.send("Successful");
res.end();
}, filter, data)
})
});
//delete student
app.delete('/delete', function (req, res) {
var filter = req.param("id");
MongoClient.connect(url, function (err, db) {
mongoIO.deleteStudent(db, function () {
db.close();
res.send('Successful');
res.end();
}, filter)
})
console.log("Delete student info")
});
// App listenning port 9999
app.listen(9999, function () {
console.log("Server is listening port 9999")
});
HungNguyenVan 14-07-2017
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

2
0
Kiểu biến trong Java Biến local Phạm vi trong phương thức, constructor, khối. Tồn tại cùng phương thưc, constructor, khối. Cần có giá trị khởi...

2
0
MongoDB Replication với Docker Bước 1: Lấy 1 image mongo về docker pull mongo Bước 2: Tạo 1 network trong docker tên là mymongocluster doc...

1
0
Đối với những chương trình đa luồng (multithreads) thường gặp những trường hợp khi nhiều luồng cùng muốn truy cập vào 1 dữ liệu nên có thể gây ra c...
Bài viết liên quan

0
5
fCC: Technical Documentation Page note So I have finished the HTML part of this exercise and I want to come here to lament about the lengthy HTML ...