註冊
註冊流程如下:
知道流程後,程式該怎麼寫呢?
app.post('/register', function (req, res) {
var user;
//TODO 用req.body.username去lowdb找資料回傳給user
//如果沒找到資料
if (!user) {
//TODO 將req.body寫進資料庫裡
res.send({
code: 200,
msg: "register successfully"
});
} else {
//TODO 用res.send({ ... });去回傳帳號已存在
}
});
完整版
app.post('/register', function (req, res) {
var user =
db.get('users')
.find({ username: req.body.username })
.value();
if (!user) {
var newUser =
db.get('users')
.push(req.body)
.last()
.assign({ id: uuid() })
.write();
console.log(req.body);
res.send({
code: 200,
msg: "register successfully"
});
} else {
res.send({
code: 409,
msg: "username is alreay existed"
});
}
});