Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
codex-avee committed Nov 4, 2015
0 parents commit 54905ab
Show file tree
Hide file tree
Showing 17 changed files with 1,311 additions and 0 deletions.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Debugging Tips

A large part of a software developers job is to find problems. To find problems in existing applications and to minimize problems in applications.

In this session we will look at tips, tricks and techniques for tracking down problems in our applications.

## We will look at:
* How to track down errors
* Analyzing stack traces
* Isolating an issue
* Debugging tools

## How to track down errors:
* Understanding the big picture
* What areas in the application are error prone
* What clues do we have?
* How to read error messages

## Analyzing stack traces:
* Which code is yours?
* At which line is the error?
* Why could things go wrong at that line?
* Silent failure?

## Isolating an issue:
* Remove code - check if it still fails
* Put code back until the error happens again
* Look at lines before & after the problem

## Debugging tools
* Chrome developer tools - break points
* print things to the console.
* break problem down into components
* use log files - with various log levels

#Setup

## Create a database

```sql
CREATE DATABASE debugging_tips;
CREATE USER debugger@localhost IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON debugging_tips.* TO debugger@localhost;
FLUSH PRIVILEGES;
```

## Create tables

```sql
create table issues (
id int not null auto_increment,
heading char(100) not null,
description text,
primary key(id)
);
```

## Install the dependencies

Using: `npm install`

## Tools for break points

[Node Inspector](https://github.com/node-inspector/node-inspector)

[Using MS Visual Code](http://stackoverflow.com/questions/30023736/mocha-breakpoints-using-visual-studio-code)
11 changes: 11 additions & 0 deletions errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Errors
======
* remove a callback from the module
* using an undefined route
* using an undefined method on a route that exists
* invalid database credentials
* can not get post
* redirect invalid
* unexpected token - no comma where there should be one
* edit the text area for the edit
* uninstall module
58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var express = require('express'),
exphbs = require('express-handlebars'),
mysql = require('mysql'),
myConnection = require('express-myconnection'),
bodyParser = require('body-parser'),
issues = require('./issues');

var app = express();

var dbOptions = {
host: 'localhost',
user: 'debugger',
password: 'passworddd',
port: 3306,
database: 'debugging_tips'
};

//setup template handlebars as the template engine
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.use(express.static(__dirname + '/public'));

//setup middleware
app.use(myConnection(mysql, dbOptions, 'single'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())

function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}

//setup the handlers
app.get('/', issues.all);
app.get('/issues', issues.all);
app.get('/issues/add' issues.showAdd);
app.get('/issues/:id', issues.get);
app.post('/issues/update/:id', issues.update);
app.post('/issuesss', issues.add);
app.get('/issues/delete/:id', issues.delete);

//this should be a post but this is only an illustration of CRUD - not on good practices
app.delete('/issues/:id', issues.delete);

app.use(errorHandler);

//configure the port number using and environment number
var portNumber = process.env.CRUD_PORT_NR || 3000;

//start everything up
app.listen(portNumber, function () {
console.log('Debugging Tips app running at port # :', portNumber);
});
62 changes: 62 additions & 0 deletions issues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var logger = require('./log');

exports.all = function (req, res) {
req.getConnection(function(err, connection){
connection.query("select * from issues", function(err, results){
res.render('issues', {issues : results})
});
});
}

exports.get = function (req, res) {
req.getConnection(function(err, connection){
connection.query("select * from issues where id = ?", req.params.id, function(err, results){
console.log(results);
return res.render('issue_edit', {issue : results[0]})
});
});

}

exports.update = function (req, res) {
req.getConnection(function(err, connection){
var data = {
heading : req.body.heading,
description : req.body.description
};

connection.query("update issues set ? where id = ?", [data, req.params.id], function(err, results){
// what will happen here?
return res.redirect("/issues");
});
});
}

exports.showAdd = function (req, res) {
res.render('isue')
}

exports.add = function (req, res) {

var data = {
heading : req.body.heading,
description : req.body.description
}
req.getConnection(function(err, connection){
// what can I do better here?
connection.query("insert into issues set ?", data, function(err, results){
// what can I do better here?
res.redirect('/issues')
});
});
}

exports.delete = function (req, res, next) {
req.getConnection(function(err, connection){
connection.query("delete from isues where id = ?", req.params.id, function(err, results){
//if (err) return next(err);
res.redirect('/issues')
});
});

}
6 changes: 6 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var winston = require('winston');

winston.add(winston.transports.File, { filename: "./logs/logger.log" });
winston.info('Chill Winston, the logs are being captured...');

module.exports=winston;
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "debugging-tips",
"version": "1.0.0",
"description": "A large part of a software developers job is to find problems. To find problems in existing applications and to minimize problems in applications.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/codex-academy/DebuggingTips.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/codex-academy/DebuggingTips/issues"
},
"homepage": "https://github.com/codex-academy/DebuggingTips#readme",
"dependencies": {
"body-parser": "^1.9.2",
"express": "^4.10.2",
"express-handlebars": "^1.1.0",
"express-myconnection": "^1.0.4",
"mysql": "^2.5.3",
"winston": "^2.0.1"
}
}
Binary file added public/.DS_Store
Binary file not shown.
Loading

0 comments on commit 54905ab

Please sign in to comment.