Last active
November 25, 2015 15:14
-
-
Save frak/7e0a0e4c92b834b62343 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var dbSession = require('./dbSession.js'); | |
var Percolator = require('percolator').Percolator; | |
var Server = function (port) { | |
var server = Percolator({'port': port, 'autoLink': false, 'staticDir': __dirname + '/../frontend'}); | |
server.route( | |
'/api/keywords', | |
{ | |
GET: function (req, res) { | |
console.log('Responding to a GET request for /api/keywords'); | |
dbSession.fetchAll( | |
'SELECT id, value, categoryID FROM keyword ORDER BY id', | |
function (err, rows) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.collection(rows).send(); | |
console.log('Response sent'); | |
} | |
} | |
); | |
}, | |
POST: function (req, res) { | |
console.log('Responding to a POST request for /api/keywords'); | |
req.onJson( | |
function (err, newKeyword) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} | |
else { | |
dbSession.query( | |
'INSERT INTO keyword (value, categoryID) VALUES (?, ?);', | |
[newKeyword.value, newKeyword.categoryID], | |
function (err, result) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.object({'status': 'ok', 'id': result.insertId}).send(); | |
} | |
} | |
); | |
} | |
} | |
); | |
} | |
} | |
); | |
server.route( | |
'/api/keywords/categories', | |
{ | |
GET: function (req, res) { | |
console.log('Responding to a GET request for /api/keywords/categories'); | |
dbSession.fetchAll( | |
'SELECT id, name FROM category ORDER BY id', | |
function (err, rows) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.collection(rows).send(); | |
} | |
} | |
); | |
} | |
} | |
); | |
return server; | |
}; | |
module.exports = {'Server': Server}; |
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
[INFO] Processed migration 20151028233622-createKeywordAndCategoryTable | |
[INFO] Done | |
Responding to a GET request for /api/keywords | |
Response sent | |
{ [Error: connect ECONNREFUSED 127.0.0.1:8080] | |
code: 'ECONNREFUSED', | |
errno: 'ECONNREFUSED', | |
syscall: 'connect', | |
address: '127.0.0.1', | |
port: 8080 } undefined | |
Error: connect ECONNREFUSED 127.0.0.1:8080 | |
at Object.exports._errnoException (util.js:874:11) | |
at exports._exceptionWithHostPort (util.js:897:20) | |
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14) |
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
'use strict'; | |
var dbSession = require('./dbSession.js'); | |
var Percolator = require('percolator').Percolator; | |
var Server = function (port) { | |
var server = Percolator({'port': port, 'autoLink': false, 'staticDir': __dirname + '/../frontend'}); | |
server.route( | |
'/api/keywords', | |
{ | |
GET: function (req, res) { | |
console.log('Responding to a GET request for /api/keywords'); | |
dbSession.fetchAll( | |
'SELECT id, value, categoryID FROM keyword ORDER BY id', | |
function (err, rows) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.collection(rows).send(); | |
console.log('Response sent'); | |
} | |
} | |
); | |
}, | |
POST: function (req, res) { | |
console.log('Responding to a POST request for /api/keywords'); | |
req.onJson( | |
function (err, newKeyword) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} | |
else { | |
dbSession.query( | |
'INSERT INTO keyword (value, categoryID) VALUES (?, ?);', | |
[newKeyword.value, newKeyword.categoryID], | |
function (err, result) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.object({'status': 'ok', 'id': result.insertId}).send(); | |
} | |
} | |
); | |
} | |
} | |
); | |
} | |
} | |
); | |
server.route( | |
'/api/keywords/categories', | |
{ | |
GET: function (req, res) { | |
console.log('Responding to a GET request for /api/keywords/categories'); | |
dbSession.fetchAll( | |
'SELECT id, name FROM category ORDER BY id', | |
function (err, rows) { | |
if (err) { | |
console.log(err); | |
res.status.internalServerError(err); | |
} else { | |
res.collection(rows).send(); | |
} | |
} | |
); | |
} | |
} | |
); | |
return server; | |
}; | |
module.exports = {'Server': Server}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment