Skip to content

Instantly share code, notes, and snippets.

@jcolemorrison
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcolemorrison/10881521 to your computer and use it in GitHub Desktop.
Save jcolemorrison/10881521 to your computer and use it in GitHub Desktop.
Building an Angular and Express App Part 2 - Express Gist
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
/**
* Route Imports
*/
var signup = require('./routes/signup');
var app = express();
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
/**
* Development Settings
*/
if (app.get('env') === 'development') {
// This will change in production since we'll be using the dist folder
// This covers serving up the index page
app.use(express.static(path.join(__dirname, '../client/.tmp')));
app.use(express.static(path.join(__dirname, '../client/app')));
}
/**
* Production Settings
*/
if (app.get('env') === 'production') {
// changes it to use the optimized version for production
app.use(express.static(path.join(__dirname, '/dist')));
}
/**
* Routes
*/
app.use('/signup', signup);
// Error Handling
app.use(function(err, req, res, next) {
res.status(err.status || 500);
});
module.exports = app;
'use strict';
angular.module('clientApp') // make sure this is set to whatever it is in your client/scripts/app.js
.controller('SignupCtrl', function ($scope, $http) { // note the added $http depedency
// Here we're creating some local references
// so that we don't have to type $scope every
// damn time
var user,
signup;
// Here we're creating a scope for our Signup page.
// This will hold our data and methods for this page.
$scope.signup = signup = {};
// In our signup.html, we'll be using the ng-model
// attribute to populate this object.
signup.user = user = {};
// This is our method that will post to our server.
signup.submit = function () {
// make sure all fields are filled out...
// aren't you glad you're not typing out
// $scope.signup.user.firstname everytime now??
if (
!user.firstname ||
!user.lastname ||
!user.email ||
!user.password1 ||
!user.password2
) {
alert('Please fill out all form fields.');
return false;
}
// make sure the passwords match match
if (user.password1 !== user.password2) {
alert('Your passwords must match.');
return false;
}
// Just so we can confirm that the bindings are working
console.log(user);
// Make the request to the server
var request = $http.post('/signup', user);
request.success(function (data) {
console.log(data.msg);
});
request.error(function (data) {
console.log(data.msg);
});
};
});
// we need express
var express = require('express');
// intialize the router
var router = express.Router();
// This is... oddly enough, mapping to the /signup.
// Even though it says '/' what it really means is the root url
// of the request it's handling. So since it was called in our
// app.use('/signup', signup); Our function here sees '/' as
// '/signup'
//
// yes... it's weird. I don't like the 4.x route better yet?
router.post('/', function (req, res) {
console.log(req.body);
res.json({
'msg': 'success!'
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment