-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5f7f63
commit 8f734cd
Showing
5 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters