Move api methods into their own folder

This commit is contained in:
Timothy Warren 2014-10-01 10:45:09 -04:00
parent c5393b050e
commit 582db56476
7 changed files with 81 additions and 55 deletions

21
app.js
View File

@ -11,7 +11,6 @@ var express = require('express'),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
requireDir = require('require-dir'), requireDir = require('require-dir'),
connection = require('express-myconnection'), connection = require('express-myconnection'),
bcrypt = require('bcrypt-nodejs'),
mysql = require('mysql2'); mysql = require('mysql2');
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
var app = express(); var app = express();
@ -71,13 +70,29 @@ app.use(function(err, req, res, next) {
// unless the filename is index. // unless the filename is index.
// //
// A '/' route in the users file becomes the '/users/' route. // A '/' route in the users file becomes the '/users/' route.
var routes = requireDir('routes'); var routes = requireDir('routes', {recurse: true});
Object.keys(routes).forEach(function(route) { Object.keys(routes).forEach(function(route) {
var path = (route != 'index') var path = (route != 'index')
? '/' + route ? '/' + route
: '/'; : '/';
app.use(path, routes[route]); var routeMethod = (typeof routes[route])
// Handle API routes
if (typeof routes[route] === 'object')
{
var innerRoute;
for (innerRoute in routes[route])
{
var innerPath = ['', route, innerRoute].join('/');
app.use(innerPath, routes[route][innerRoute]);
}
}
else
{
app.use(path, routes[route]);
}
}); });
// catch 404 and forward to error handler // catch 404 and forward to error handler

5
public/js/site.js Normal file
View File

@ -0,0 +1,5 @@
(function() {
"use strict";
})();

View File

@ -19,55 +19,10 @@ router.get('/', function(req, res) {
res.render('index', { res.render('index', {
title: 'Node Task Manager', title: 'Node Task Manager',
user: req.session.username, user: req.session.username,
req: request req: request,
loggedin: true
}); });
} }
}); });
/* Login action */
router.post('/login', function(req, res) {
var bcrypt = require('bcrypt-nodejs');
var user = req.body.user,
pass = req.body.pass;
req.getConnection(function(err, connection) {
if (err) throw err;
var sql = " SELECT id, username, email, password, timezone, num_format " +
" FROM todo_user " +
" WHERE email = ? OR username = ? ";
// Find the username / email
connection.execute(sql, [user, user], function(err, rows, fields) {
if (err) throw err;
var user = rows[0];
// Verify the password hash
bcrypt.compare(pass, user.password, function(err, passRes) {
if (err) throw err;
// Password is good, set session data and redirect
if (passRes === true)
{
req.session.uid = user.id;
req.session.num_format = user.num_format;
req.session.username = user.username;
res.redirect(303, '/');
}
});
});
})
});
/* Logout action */
router.get('/logout', function(req, res) {
// Destroy the session, and redirect to the index page
req.session.destroy(function(err) {
res.redirect(303, '/');
});
});
module.exports = router; module.exports = router;

View File

@ -1,6 +1,52 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
/* Login action */
router.post('/login', function(req, res) {
var bcrypt = require('bcrypt-nodejs');
var user = req.body.user,
pass = req.body.pass;
req.getConnection(function(err, connection) {
if (err) throw err;
var sql = " SELECT id, username, email, password, timezone, num_format " +
" FROM todo_user " +
" WHERE email = ? OR username = ? ";
// Find the username / email
connection.execute(sql, [user, user], function(err, rows, fields) {
if (err) throw err;
var user = rows[0];
// Verify the password hash
bcrypt.compare(pass, user.password, function(err, passRes) {
if (err) throw err;
// Password is good, set session data and redirect
if (passRes === true)
{
req.session.uid = user.id;
req.session.num_format = user.num_format;
req.session.username = user.username;
res.redirect(303, '/');
}
});
});
})
});
/* Logout action */
router.get('/logout', function(req, res) {
// Destroy the session, and redirect to the index page
req.session.destroy(function(err) {
res.redirect(303, '/');
});
});
/* GET users listing. */ /* GET users listing. */
router.get('/list', function(req, res) { router.get('/list', function(req, res) {
res.send('respond with a resource'); res.send('respond with a resource');

View File

@ -5,13 +5,16 @@
<link rel="stylesheet" href="/css/ink.min.css" /> <link rel="stylesheet" href="/css/ink.min.css" />
<link rel="stylesheet" href="/css/ink-flex.min.css" /> <link rel="stylesheet" href="/css/ink-flex.min.css" />
<link rel="stylesheet" href="/css/font-awesome.min.css" /> <link rel="stylesheet" href="/css/font-awesome.min.css" />
<script src="/js/ink-all.min.js"></script>
</head> </head>
<body> <body>
<header class="ink-grid"> <header class="ink-grid">
<h1>{title}</h1> <h1>{title}</h1>
<nav class="ink-navigation"> <nav class="ink-navigation">
<ul class="menu horizontal"> <ul class="pagination pills green">
<li><a href="/logout">Logout</a></li> {#loggedin}
<li><a href="/user/logout">Logout</a></li>
{/loggedin}
</ul> </ul>
</nav> </nav>
</header> </header>

View File

@ -1,7 +1,7 @@
{>layout/} {>layout/}
{<content} {<content}
<form class="ink-form column-group" action="/login" method="post"> <form class="ink-form column-group all-75 tiny-100" action="/user/login" method="post">
<fieldset> <fieldset>
<legend>Login</legend> <legend>Login</legend>
<div class="control-group required"> <div class="control-group required">
@ -20,7 +20,9 @@
</div> </div>
<div class="control-group"> <div class="control-group">
<input type="hidden" name="_csrf" value="{csrfToken}" /> <input type="hidden" name="_csrf" value="{csrfToken}" />
<button type="submit">Login</button> <div class="control">
<button class="ink-button green" type="submit">Login</button>
</div>
</div> </div>
</fieldset> </fieldset>
</form> </form>