samedi 10 octobre 2015

How to correctly deploy an app (nodeJS) to AWS

I had written a previous post, but, maybe I was a bit too vague and/or unclear, so, I'll try my best to explain what I want exactly and what I've done so far to try to achieve it:

I have a very simple application that I've developed using the IDE WebStorm and the MEAN stack.

There are some steps I took to pass it on from my localhost to the web (AWS in particular):

1) I've set up a MongoDB database in MongoLab, so, my "connection string" in the code resembles something like:

var mongoose = require("mongoose");

mongoose.connect("mongodb://USER:PASS@ds041643.mongolab.com:41643/MYDB");

So, as you can all see, I'm not using any DB service provided by AWS as I believe it shouldn't be mandatory (I might be completely wrong).

2) Obviously, all routes and connections that are related to the server code, went from:

"http://localhost:3000/ASAMPLEROUTE" to simply:

"/ASAMPLEROUTE"

3) The application went from listening on port 3000, to listen on either: process.env.PORT OR a static higher port, like: 8080.

4) The folder structure inside the repository was the most complicated for me to manage:

The name of the server file MUST be server.js and it must exist in the root directory of my app. All the other files and/or folders like the ones responsible for using libraries like RaphaelJS, connecting to the DB or containing the "front-end" code, can be placed in their respective places, as I wish?

For instance, would this repository structure be suited for a successful deployment:

enter image description here

  • db folder: contains the files needed to connect to my MongoLab DB and the models I'm using in my app. I reference these files inside the file server.js to save/get things to/from the DB;
  • public folder: contains all the HTML pages, a sub-folder called bower_components that contains angularJS and the JS file called app.js which is the file that will allow interaction and "to get things moving" visually, namely drawing things using RaphaelJS library and AngularJS and respond to button clicks, etc. It will send requests in the form of POST/GET to the "server-side" namely the DB;
  • styles folder: Referenced on most HTML pages, contains some CSS/Bootstrap files needed to make the layout and the application look "pretty". Note here that I am using some CSS files grabbed from CDN like bootstrapCDN;
  • node_modules folder: contains all the dependencies needed for the project to run, like moongose, nodemailer, express, etc.
  • static files server.js & Raphael.js: simply the RaphaelJS library and the server file that I need to keep running on background "forever" (I'm aware of the nodejitsu module forever) so that the application can actually be "backed up" by a working server. This should be running constantly so the application can work;

EXTRA STUFF I NEED AND HAVE:

1) The github repo is private, so, in order to connect to it remotely, I need a SSH Key-pair that can be generated in AWS console and saved in the same folder that you can see above. This is needed because the repo is private, and we need to access it without password issues;

2) File package.json - the file that "acknowledges" an app as being a nodeJS one, where all the dependencies are listed and start scripts/specific versions of some component are specified. In my case, it looks like this:

{
  "name": "SSED",
  "version": "0.0.0",
  "dependencies": {
    "body-parser": "~1.14.0",
    "cors": "~2.7.1",
    "express": "~4.13.3",
    "grunt": "~0.4.5",
    "nodemailer": "~0.6.5",
    "mongoose": "~4.1.7",
    "xmlbuilder": "~3.1.0"
  },
"scripts" : {
   "start" : "server.js"
},
"engines" : {
    "node": "0.10.25"
  },
  "repository": {
    "type": "git",
    "url": "http://ift.tt/1WTxXPh"
  }
}

Now for the real questions:

Services to use provided by Amazon:

Question 1: What should I use to accomplish the deployment of this app; with an external DB and a nodeJS server file continously running?

Possible Ans: I have only looked at 2 services:

1) The EC2 - Amazon Elastic Compute Cloud;

2) The OpsWorks (under Management tools) service, which seemed good to me because it would allow me to put my server running or so I think, but I never got it to work;

Question 2: Is it possible to deliver content from A CDN to render the CSS in the AWS platform?

Question 3: The colleague I've worked with, managed to make the CSS and AngularJS 100% functional using this: http://ift.tt/1jhtHL8 but the server file failed to run there, even after we download the entire github repo into there, install nodeJS and run the typical command:

node server.js

There was a version with the words (HVM) in brackets, but we dont know what that is.

If you could give us some tips, that'd be extremely helpful, especially because we are paying for this, it's our first time using this and it's for a Master thesis due to November, so we're seeing our lives going backwards.




Aucun commentaire:

Enregistrer un commentaire