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'),
requireDir = require('require-dir'),
connection = require('express-myconnection'),
bcrypt = require('bcrypt-nodejs'),
mysql = require('mysql2');
// ----------------------------------------------------------------------------
var app = express();
@ -71,13 +70,29 @@ app.use(function(err, req, res, next) {
// unless the filename is index.
//
// 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) {
var path = (route != 'index')
? '/' + route
: '/';
var routeMethod = (typeof routes[route])
app.use(path, 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

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

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

View File

@ -5,7 +5,7 @@ var router = express.Router();
router.get('/', function(req, res) {
var util = require('util');
var request = util.inspect(req, {depth: 2});
// If the user isn't logged in
if ( ! req.session.uid)
{
@ -19,55 +19,10 @@ router.get('/', function(req, res) {
res.render('index', {
title: 'Node Task Manager',
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;

View File

@ -1,6 +1,52 @@
var express = require('express');
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. */
router.get('/list', function(req, res) {
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-flex.min.css" />
<link rel="stylesheet" href="/css/font-awesome.min.css" />
<script src="/js/ink-all.min.js"></script>
</head>
<body>
<header class="ink-grid">
<h1>{title}</h1>
<nav class="ink-navigation">
<ul class="menu horizontal">
<li><a href="/logout">Logout</a></li>
<ul class="pagination pills green">
{#loggedin}
<li><a href="/user/logout">Logout</a></li>
{/loggedin}
</ul>
</nav>
</header>

View File

@ -1,7 +1,7 @@
{>layout/}
{<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>
<legend>Login</legend>
<div class="control-group required">
@ -20,7 +20,9 @@
</div>
<div class="control-group">
<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>
</fieldset>
</form>