From 8f734cd8cb18aaed0c8d60720f93ce52c2367d43 Mon Sep 17 00:00:00 2001 From: hungle Date: Tue, 26 Jul 2016 13:42:23 +0900 Subject: [PATCH] BACKEND API update & auth --- README.md | 11 ++++- backend/API.js | 98 +++++++++++++++++++++++++++++---------- package.json | 3 +- sample/API/api_default.js | 28 +++++++++++ sample/mongodb/connect.js | 14 +++--- 5 files changed, 121 insertions(+), 33 deletions(-) create mode 100644 sample/API/api_default.js diff --git a/README.md b/README.md index 895ea17..2f17f35 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,16 @@ npm install mongodb --save - load UserModel on app.js (window.onload) ### Update Backend API -- +``` +npm install sha1 --save +``` +- API request add record to db +- Connect to mongodb (common) + - mongodb helper: getDatabaseConnection -> connect to mongodb + - connect the first time (save to cache as object for the nex call) +- Fetching the POST data - Request handling (common) +- Router api/user/add method + ### User authentication with sessions diff --git a/backend/API.js b/backend/API.js index f0f557c..66221d4 100644 --- a/backend/API.js +++ b/backend/API.js @@ -1,28 +1,78 @@ -// backend/API.js -module.exports = function(req, res) { - res.writeHead(200, {'Content-Type': 'application/json'}); - res.end('{}' + '\n'); -} +// mongodb +var MongoClient = require('mongodb').MongoClient; +var database; -var response = function(result, res) { - res.writeHead(200, {'Content-Type': 'application/json'}); - res.end(JSON.stringify(result) + '\n'); -} +// mongodb helper: getDatabaseConnection -> connect to mongodb +// connect the first time (save to cache as object for the nex call) +var getDatabaseConnection = function(callback) { + if(database) { + callback(database); + return; + } else { + MongoClient.connect('mongodb://127.0.0.1:27017/nodejs-example', + function(err, db) { + if(err) { + throw err; + }; + database = db; + callback(database); + }); + } +}; -var Router = require('../frontend/js/lib/router')(); +// POST request handling +var querystring = require('querystring'); +var processPOSTRequest = function(req, callback) { + var body = ''; + req.on('data', function (data) { + body += data; + }); + req.on('end', function () { + callback(querystring.parse(body)); + }); +}; -Router -.add('api/version', function(req, res) { - response({ - version: '0.1' - }, res); -}) -.add(function(req, res) { - response({ - success: true - }, res); -}); +// email validation +var validEmail = function(value) { + var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(value); +}; -module.exports = function(req, res) { - Router.check(req.url, [req, res]); -} +// router api/user with add method +Router.add('api/user', function(req, res) { + switch(req.method) { + case 'GET': + // ... + break; + case 'PUT': + // ... + break; + case 'POST': + processPOSTRequest(req, function(data) { + if(!data.firstName || data.firstName === '') { + error('Please fill your first name.', res); + } else if(!data.lastName || data.lastName === '') { + error('Please fill your last name.', res); + } else if(!data.email || data.email === '' || + !validEmail(data.email)) { + error('Invalid or missing email.', res); + } else if(!data.password || data.password === '') { + error('Please fill your password.', res); + } else { + getDatabaseConnection(function(db) { + var collection = db.collection('users'); + data.password = sha1(data.password); + collection.insert(data, function(err, docs) { + response({ + success: 'OK' + }, res); + }); + }); + } + }); + break; + case 'DELETE': + // ... + break; + }; +}); diff --git a/package.json b/package.json index 6605ebd..fdfb7ff 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "gulp-uglify": "^1.5.4", "mongodb": "^2.2.4", "ractive": "^0.7.3", - "reactive": "^1.2.0" + "reactive": "^1.2.0", + "sha1": "^1.1.1" }, "author": "lecaoquochung@gmail.com", "license": "MIT" diff --git a/sample/API/api_default.js b/sample/API/api_default.js new file mode 100644 index 0000000..f0f557c --- /dev/null +++ b/sample/API/api_default.js @@ -0,0 +1,28 @@ +// backend/API.js +module.exports = function(req, res) { + res.writeHead(200, {'Content-Type': 'application/json'}); + res.end('{}' + '\n'); +} + +var response = function(result, res) { + res.writeHead(200, {'Content-Type': 'application/json'}); + res.end(JSON.stringify(result) + '\n'); +} + +var Router = require('../frontend/js/lib/router')(); + +Router +.add('api/version', function(req, res) { + response({ + version: '0.1' + }, res); +}) +.add(function(req, res) { + response({ + success: true + }, res); +}); + +module.exports = function(req, res) { + Router.check(req.url, [req, res]); +} diff --git a/sample/mongodb/connect.js b/sample/mongodb/connect.js index 77ca533..e5f8b03 100644 --- a/sample/mongodb/connect.js +++ b/sample/mongodb/connect.js @@ -7,10 +7,10 @@ function(err, db) { }); // db object with CRUD -var collection = db.collection('users'); -collection.insert({ - name: 'Hung', - email: 'lehung@test.com' -}, function(err, result) { - // ... -}); + // var collection = db.collection('users'); + // collection.insert({ + // name: 'Hung', + // email: 'lehung@test.com' + // }, function(err, result) { + // // ... + // });