Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Database handler #34

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
53 changes: 53 additions & 0 deletions Database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var mysql = require('mysql');
// the Database connection information goes here
var dbConfig = {
host: "",
user: "",
password: "",
database: ""
};

var con;

function query(sql,values,callback){
con = mysql.createConnection(dbConfig);
con.query(sql, [values], function (err, result,fields) {
if(err)
throw err;
else
callback(result);
});
con.end();
}

module.exports = {

exampleUsage: function(body,callback){
var sql = "";
var params = [];
query(sql,param,callback);
},

createUser : function(body,callback){

},

getUser : function (params,callback) {
var sql = "select * from user where id = ?";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tables should have plural names

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

var values= [params.id];
console.log("Pre query")
query(sql,values,callback);
},

GiveRoleToUser : function (body,callback) {

},

CreatePost : function (body,callback) {

},

AddTagToPost : function (body,callback) {

}
}
118 changes: 82 additions & 36 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,89 @@ var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cluster = require('cluster');
var debug = require('debug')('dapi:server');
var http = require('http');

var index = require('./routes/index');
var users = require('./routes/users');

//----- API pages -------
var index = require('./endpoints/index');
var users = require('./endpoints/users');

var app = express();
var port = 3000;

if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;

// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}

//Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker
cluster.fork();
});
} else {
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}

// respond with json
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}

// default to plain-text. send()
res.type('txt').send('Not found');
});

app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

console.log('Application running!');

// create HTTP-server
var server = http.createServer(app);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
// listen to port
server.listen(port);
}
90 changes: 0 additions & 90 deletions bin/www

This file was deleted.

File renamed without changes.
13 changes: 13 additions & 0 deletions endpoints/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var express = require('express');
var router = express.Router();
var db = require('../Database');

/* GET a user from the database. */
router.get('/:id', function(req, res, next) {
db.getUser(req.query,function (callback) {
res.writeHead(200,{'Content-Type':'application/json'});
res.end(JSON.stringify(callback));
})
});

module.exports = router;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"start": "nodejs app.js",
"test": "mocha"
},
"dependencies": {
Expand Down
Empty file removed public/.gitkeep
Empty file.
9 changes: 0 additions & 9 deletions routes/users.js

This file was deleted.

23 changes: 18 additions & 5 deletions test/mock.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
const assert = require('assert');
const db = require('../Database');

describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
});

describe('#Database.js', function () {
it('Should return a row containing all the information of a user', function () {
var query = {"id":"1"};
var answer = 0;
db.getUser(query,function (callback) {
answer = callback;
});
assert.equal(1,answer);
});
});