/PollApp

Primary LanguageJavaScriptMIT LicenseMIT

Poll App — Learning by building AngularJS app

This project is an application for a typical polls web app.You can use it to quickly create a poll ,vote for a poll and also view results of a poll.

The PollApp is a simple AngularJS application and is developed as a part of reddit r/JsTogether projects. The seed app doesn't do much, just shows how to wire two controllers and views together.

Getting Started

###Commands Used to set up the project to get start: cloning Angular-seed and creating a fresh project

git clone --depth=1 https://github.com/angular/angular-seed.git PollApp
cd angular-seed
rm -rf .git
git init
git add .
git commit -m "new"
git remote add origin https://github.com/pknelakuditi/PollApp.git
git push -u origin master

heroku deployment: https://scotch.io/tutorials/how-to-deploy-a-node-js-app-to-heroku

create the remote repository heroku create app-name

Deploying Code git push heroku master

Ensure One Instance Is Running heroku ps:scale web=1

You can push an alternative branch to Heroku using Git. git push heroku pknFeature:master

Rename : heroku apps:rename newname --app oldname

create a Procfile in the root of your project web: node server.js

useful commands

Dependencies

We have two kinds of dependencies in this project: tools and angular framework code. The tools help us manage and test the application.

  • We get the tools we depend upon via npm, the [node package manager][npm].
  • We get the angular code via bower, a [client-side code package manager][bower].

We have preconfigured npm to automatically run bower so we can simply do:

npm install

Behind the scenes this will also call bower install. You should find that you have two new folders in your project.

  • node_modules - contains the npm packages for the tools we need
  • app/bower_components - contains the angular framework files

Note that the bower_components folder would normally be installed in the root folder but angular-seed changes this location through the .bowerrc file. Putting it in the app folder makes it easier to serve the files by a webserver.

Run the Application

We have preconfigured the project with a simple development web server. The simplest way to start this server is:

npm start

Now browse to the app at http://localhost:8000/app/index.html.

Intial Directory Layout

app/                    --> all of the source files for the application
  app.css               --> default stylesheet
  components/           --> all app specific modules
    version/              --> version related components
      version.js                 --> version module declaration and basic "version" value service
      version_test.js            --> "version" value service tests
      version-directive.js       --> custom directive that returns the current app version
      version-directive_test.js  --> version directive tests
      interpolate-filter.js      --> custom interpolation filter
      interpolate-filter_test.js --> interpolate filter tests
  view1/                --> the view1 view template and logic
    view1.html            --> the partial template
    view1.js              --> the controller logic
    view1_test.js         --> tests of the controller
  view2/                --> the view2 view template and logic
    view2.html            --> the partial template
    view2.js              --> the controller logic
    view2_test.js         --> tests of the controller
  results/              --> the results view template and logic
    results.html          --> the partial template
    results.js            --> the controller logic
  css/                  --> additional css
    helper.css            --> css for graphs
  app.js                --> main application module
  index.html            --> app layout file (the main html template file of the app)
  index-async.html      --> just like index.html, but loads js files asynchronously
karma.conf.js         --> config file for running unit tests with Karma
e2e-tests/            --> end-to-end tests
  protractor-conf.js    --> Protractor config file
  scenarios.js          --> end-to-end scenarios to be run by Protractor
server.js             --> express server
routes/               --> routes
  pollRoute.js          --> api endpoint for polls
  indexRoute.js         --> index 
helper/               --> helper funcitons for server.js
  pollValidator.js      --> server validation of polls
  voteValidator.js      --> validate user vote

Updating Angular

Previously we recommended that you merge in changes to angular-seed into your own fork of the project. Now that the angular framework library code and tools are acquired through package managers (npm and bower) you can use these tools instead to update the dependencies.

You can update the tool dependencies by running:

npm update

This will find the latest versions that match the version ranges specified in the package.json file.

You can update the Angular dependencies by running:

bower update

This will find the latest versions that match the version ranges specified in the bower.json file.

Loading Angular Asynchronously

The angular-seed project supports loading the framework and application scripts asynchronously. The special index-async.html is designed to support this style of loading. For it to work you must inject a piece of Angular JavaScript into the HTML page. The project has a predefined script to help do this.

npm run update-index-async

This will copy the contents of the angular-loader.js library file into the index-async.html page. You can run this every time you update the version of Angular that you are using.

Serving the Application Files

While angular is client-side-only technology and it's possible to create angular webapps that don't require a backend server at all, we recommend serving the project files using a local webserver during development to avoid issues with security restrictions (sandbox) in browsers. The sandbox implementation varies between browsers, but quite often prevents things like cookies, xhr, etc to function properly when an html page is opened via file:// scheme instead of http://.

Running the App during Development

npm start

Running the App in Production

This really depends on how complex your app is and the overall infrastructure of your system, but the general rule is that all you need in production are all the files under the app/ directory. Everything else should be omitted.

Angular apps are really just a bunch of static html, css and js files that just need to be hosted somewhere they can be accessed by browsers.

If your Angular app is talking to the backend server via xhr or other means, you need to figure out what is the best way to host the static files to comply with the same origin policy if applicable. Usually this is done by hosting the files by the backend server or through reverse-proxying the backend server(s) and webserver(s).