This template helps make simple apps (like Changes in Crime by Neighborhood and A License to Blight) by baking out EJS templates into HTML, compiling SASS to CSS, linting, concatenating, and minifying (but not obfuscating!) JS, and versioning assets.
We got a lot of inspiration and guidance from the NPR Apps app-template.
This branch is setup for making fullscreen maps, which means Leaflet is included, there are helper functions in app.js, and basic layout of document is a map canvas with a header and map controls.
css- compiled .scss files for developmentcss\lib- vendor css filesdata- Raw data, i.e. CSVsjs- development versions of JS filesjs\lib- vendor/library JS files, i.e. underscore.js, backbone.js, etc.img- imagesimg\lib- images for vendor libraries, i.e. leaflet marker imagesass- .scss filesscripts- miscellaneous scripts used for data processing, etc.views- EJS templateswww,www\css,www\js, etc - The compiled app and associated assetswww\data- Processed data (i.e. JSON) for the app.build.js- Script to generate static HTML files fromserver.jsroutes. Called by Gruntshell:buildtask.Gruntfile.js- Grunt file with tasks, i.e. lint, deploy, etc.package.json- Node project file. We use thenameattribute for the production url (i.e. http://apps.axisphilly.org/package-name) and theversionattribute to version production assets.server.js- Express server used for development
For asset management, static view compiling, and building, we use Node.js and Grunt.
On OS X, you can use Homebrew to install Node: $ brew install node
There is also an install package for OS X and other systems available on the Node website.
Install the Grunt command line tool globally, with the command line interface: $ npm install -g grunt-cli
Install project dependencies (in the project folder): $ npm install
For local development, we use an Express server to serve files, compile SASS and render views.
To run the server: $ node server.js
Then go to http://0.0.0.0:3000 in your browser.
Adding a new page to the app is as simple as adding a route to the Express server and assigning it a view.
-
Add a new route to Express. At the very least, you have to pass the environment variable to the view, in order to reference development/production resources respectively. For example:
app.get('/route-name', function(req, res){ res.render('view-name', { env: app.settings.env }); }); -
Then, create a new view in the
viewfolder. The view name is the first parameter of theres.rendermethod. The view can just be anhtmlfile, or it can use EJS templating to be more dynamic. Just make sure you pass the EJS variables to the route; which is the second parameter of theres.rendermethod.
grunt build compiles your project in production mode. It will lint, concatenate, and minify JS files, bake-out the EJS templates into HTML, and compile SASS to CSS.
$ grunt build
Make sure any data needed for the app is placed in www\data. Ideally, any data processing scripts should save output here.
The grunt commands can also be run independently:
- Lint JS files:
$ grunt jshint - Concatenate and minify JS files:
$ grunt uglify - Compile SASS to CSS:
$ grunt sass - Bake-out template files:
$ grunt shell
Deployment to S3 is handled by grunt. Before you deploy, do the following:
- The value of the
namekey inpackage.jsonwill be used as the S3 folder name, so make sure it's URL compliant. - Do not add the AWS credentials to the Gruntfile. Grunt expects environmental variables stored as
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY. Our convention is to store them in a file called.env, which you can thensourceto load into your environment. - Check all the of
srcanddestvalues in the s3 grunt taskuploadkey to make sure they are valid. The defaults arewww/*,www/js/*,www/css/*, andwww/data/*. Basically, they should match the folder structure ofwww.
Once you checked all of the above, you can deploy the app by running:
$ grunt deploy