-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] split http request handlers out into their own file to simpli…
…fy server.js took 4 mins. preparing for #23
- Loading branch information
Showing
5 changed files
with
81 additions
and
61 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
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,8 +1,9 @@ | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>Time MVP</title> | ||
<link rel="shortcut icon" type="image/png" href="http://www.favicon.cc/logo3d/818488.png"/> | ||
<title>Time MVP (Title Gets Over-ridden with Clock ;-)</title> | ||
<link rel="shortcut icon" type="image/png" | ||
href="https://cloud.githubusercontent.com/assets/194400/25605640/15c23162-2f04-11e7-8371-228cf5bf61e2.png"/> | ||
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/tachyons.min.css"/> | ||
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> | ||
<script src="client.js"></script> <!-- load "client" application --> | ||
|
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,62 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var db = require('./db.js'); | ||
|
||
function serve_index(req, res) { | ||
return fs.readFile(path.resolve('./lib/index.html'), function (err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(data); | ||
}); | ||
} | ||
|
||
function serve_client(req, res) { | ||
return fs.readFile(path.resolve('./lib/client.js'), function (err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/javascript'}); | ||
res.end(data); | ||
}); | ||
} | ||
|
||
function handle_post(req, res) { | ||
var body = ''; // accumulate the HTTP POST body before attempting to process. | ||
req.on('data', function (data) { body += data; }); | ||
req.on('end', function () { | ||
try { | ||
req.json = JSON.parse(body); // MVP!! (Don't Do this Kids!!) | ||
} catch(e) { // in case for any reason the JSON from the client is malformed | ||
console.warn('unable to parse the data received:', body) | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
return res.end(body); | ||
} | ||
db.save_state(req, res, function(err, req, res){ | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
return res.end(JSON.stringify(req.json)); | ||
}); | ||
}); | ||
} | ||
|
||
function handle_email_verification_request(req, res) { | ||
if(req.headers.accept.indexOf('text/html') > -1) { // clicked link in email | ||
db.check_verification_token(req, res, function(err, req, res) { | ||
fs.readFile(path.resolve('./lib/index.html'), function (err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(data); | ||
}); | ||
}); | ||
} | ||
else { // get existing state for client based on verification token | ||
console.log('JSON "AJAX" request for /verify'); | ||
db.get_store_for_verification_token(req, res, function(err, req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
console.log('63 - - - - - - - - - - - -req.json:') | ||
console.log(req.json) | ||
return res.end(JSON.stringify(req.json)); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
serve_index: serve_index, | ||
serve_client: serve_client, | ||
handle_post: handle_post, | ||
handle_email_verification_request: handle_email_verification_request | ||
} |
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,68 +1,21 @@ | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var db = require('./db.js'); | ||
var handlers = require('./request_handlers.js'); | ||
|
||
http.createServer(function handler (req, res) { | ||
console.log(req.method, ':', req.url); | ||
url = req.url.split('?')[0]; | ||
http.createServer(function handler (req, res) { // can you make it simpler? ;-) | ||
console.log(req.method, ':', req.url); // rudimentary request logging | ||
var url = req.url.split('?')[0]; // strip query params for url routing | ||
switch (url) { | ||
case '/save': | ||
handle_post(req, res) | ||
handlers.handle_post(req, res); | ||
break; | ||
case '/verify': | ||
handle_email_verification_request(req, res); | ||
handlers.handle_email_verification_request(req, res); | ||
break; | ||
case '/client.js': | ||
fs.readFile(path.resolve('./lib/client.js'), function (err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/javascript'}); | ||
res.end(data); | ||
}); | ||
handlers.serve_client(req, res); | ||
break; | ||
default: | ||
fs.readFile(path.resolve('./lib/index.html'), function (err, data) { | ||
// console.log('accept:', req.headers.accept); | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(data); | ||
}); | ||
handlers.serve_index(req, res); | ||
break; | ||
} | ||
}).listen(process.env.PORT); // start the server with the command: npm run dev | ||
|
||
function handle_post(req, res) { | ||
var body = ''; // accumulate the HTTP POST body before attempting to process. | ||
req.on('data', function (data) { body += data; }); | ||
req.on('end', function () { | ||
try { | ||
req.json = JSON.parse(body); // MVP!! (Don't Do this Kids!!) | ||
} catch(e) { // in case for any reason the JSON from the client is malformed | ||
console.warn('unable to parse the data received:', body) | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
return res.end(body); | ||
} | ||
db.save_state(req, res, function(err, req, res){ | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
return res.end(JSON.stringify(req.json)); | ||
}); | ||
}); | ||
} | ||
|
||
function handle_email_verification_request(req, res) { | ||
if(req.headers.accept.indexOf('text/html') > -1) { // clicked link in email | ||
db.check_verification_token(req, res, function(err, req, res) { | ||
fs.readFile(path.resolve('./lib/index.html'), function (err, data) { | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(data); | ||
}); | ||
}); | ||
} | ||
else { // get existing state for client based on verification token | ||
console.log('JSON "AJAX" request for /verify'); | ||
db.get_store_for_verification_token(req, res, function(err, req, res) { | ||
res.writeHead(200, {'Content-Type': 'text/json'}); | ||
console.log('63 - - - - - - - - - - - -req.json:') | ||
console.log(req.json) | ||
return res.end(JSON.stringify(req.json)); | ||
}); | ||
} | ||
} |