Monday, 22 May 2017

How to integrate Nodejs running on AWS EC2 and static Angularjs content on AWS S3?

I have a Nodejs and Angularjs application running on a single AWS EC2 instance. The code structure of the application is as follows :

Code Structure

Currently, when a user makes the first call to the application, the call is handled by nodejs express framework and the call is directed to the index.html page within angular static directory, using the following code in server/server.js file

res.sendFile(path.resolve(__dirname + '/../web/dist/index.html'));

From index.html, various static components (JS, HTML, CSS etc) are invoked and all angularjs controllers / services make a call back to nodejs API for fulfilling user request.

This arrangement works fine in single instance. Now I want to move the angular static content (i.e. web directory to AWS S3). In doing so, do I have to change the following code in server.js to :

res.sendFile(path.resolve('**AWS S3 URL** +/web/dist/index.html'));

as my static files have now moved to AWS S3. Also, in doing so I have to modify all angular controllers and service to use absolute path to Nodejs API call. This means making lots of changes and introducing deployment configuration (which we have to anyways at a later date for flexibility).

Another approach that we can take is move index.html from web to server folder by making the following changes in server/server.js :

res.sendFile(path.resolve(__dirname + '/../server/index.html'));

then add the following code in server.js to handle redirects for static contents from Nodejs to S3 , something like this :

app.get('/assets/*', function(req, res){
    var requestURL = req.url;
    var redirectedURL = <<**Remote S3 base URL**>> + requestURL;
    res.redirect(redirectedURL);
});

However, the concern is there will be lots of redirects and I am wondering, if that is a good design? I have read that search engines do not like applications with lots of redirects (HTTP 301, 304) and rank those pages lower. Hence I am trying to find out what is the best practice when deploying nodejs on AWS EC2 and static angularjs contents on S3. Any advice will be highly appreciated.



via Dev

No comments:

Post a Comment