Express with AWS Elastic Beanstalk

2018-03-04

Initialize the Project and Beanstalk

  1. Initialize a express project

  2. cd into that project

  3. git init

  4. add the following content to .gitignore

    1
    2
    3
    node_modules/
    .gitignore
    .elasticbeanstalk/
  5. Create a repository with the eb init command.

    1
    eb init --platform node.js --region us-west-2
  6. Create an environment running a sample application with the eb create command.

    1
    eb create --sample node-express-env
  7. When environment creation completes, use the eb open command to open the environment’s URL in the default browser.

    1
    eb open

Update the Application

  1. Add the following content to .ebextensions/nodecommand.config

    1
    2
    3
    option_settings:
    aws:elasticbeanstalk:container:nodejs:
    NodeCommand: "npm start"
  2. Stage the files:

    1
    2
    git add .
    git commit -m "First express app"
  3. Deploy the changes:

    1
    eb deploy

Configure static files and add a new page to your Express Application

  1. Add the following content to .ebextensions/staticfiles.config

    1
    2
    3
    option_settings:
    aws:elasticbeanstalk:container:nodejs:staticfiles:
    /public: public
  2. Comment the following line in app.js

    1
    // app.use(express.static(path.join(__dirname, 'public')));
  3. Add your updated files to your local repository and commit your changes.

    1
    2
    git add .ebextensions/ app.js
    git commit -m "Serve stylesheets statically with nginx."
  4. Add node-express/routes/hike.js. Type the following:

    1
    2
    3
    4
    5
    6
    exports.index = function(req, res) {
    res.render('hike', {title: 'My Hiking Log'});
    };
    exports.add_hike = function(req, res) {
    };
  5. Update node-express/app.js to include three new lines.

    First, add the following line to add a require for this route:

    1
    2
    3
    var express = require('express');
    var path = require('path');
    var hike = require('./routes/hike'); //add this
  6. Then, add the following two lines to node-express/app.js after var app = express();

    1
    2
    3
    var app = express();
    app.get('/hikes', hike.index); //add this
    app.post('/add_hike', hike.add_hike); //add this
  7. Copy node-express/views/index.jade to node-express/views/hike.jade.

    1
    cp views/index.jade views/hike.jade
  8. Add your files to the local repository, commit your changes, and deploy your updated application.

    1
    2
    3
    git add .
    git commit -m 'Add hikes route and template'
    eb deploy

Clean Up

1
eb terminate

Comments: