Deploying CloudFront in front of NodeJS Express - node.js

I have an app which serves a dynamic html and some static files written in NodeJS/Express.
I deployed an AWS CloudFront distro in front of it, however only the HTML goes through, all the static files result in 404. The headers of the requests look like:
Age:116
Connection:keep-alive
Content-Length:170
Content-Security-Policy:default-src 'self'
Content-Type:text/html; charset=utf-8
Date:Mon, 09 Oct 2017 14:37:23 GMT
Server:nginx/1.6.2
Via:1.1 523db8f46d98334ac6b5debbf315e15b.cloudfront.net (CloudFront), 1.1 proxy1.xxx.yy (squid/4.0.17)
X-Amz-Cf-Id:5UYpluGn8TxUxsxmmDYYiZnjbOWbZ7iFFit55mmgcN6IbAJHCEAX6Q==
X-Cache:MISS from proxy1.xxx.yy
X-Cache:Error from cloudfront
X-Cache-Lookup:MISS from proxy1.xxx.yy:3128
X-Content-Type-Options:nosniff
X-Powered-By:Express
For info, my nodejs app runs in some port, and nginx reverse-proxies it to the domain I specified using proxy_pass.
As you can see I'm behind another proxy, but this cannot be the problem.
What I believe is happening is that my origin looks like mydomain.com/path/app_id and express serves static files from mydomain.com/.
Has anyone successfully deployed CloudFront in front of NodeJS/Express for static files? I really don't understand what the problem is..
Thanks!

To serve files from another path the flow is as follows:
Add all necessary origins (in this case mydomain.com/path/app_id AND mydomain.com (without trailing /))
Add behaviours on your ditribution for every file type. In this case static files are stored in different folders (like /img). So we can add behaviours for img/*, js/* and css/*. Then each behaviour can be set to a single origin. In this case we choose mydomain.com which was previously named Static files origin.

Related

How to static-serve all urls without '/' at the end except certain ones using express?

I am setting up a Node.JS application using express, and I want domain.tld/hey to serve the public/index/index.html folder on the server (also containing other files such as .css or .js files).
However, I DO NOT want domain.tld/hey/ to work (the / at the end is a problem for me) and in this case, I want to display a custom error page located at public/error/index.html on the server.
Finally, when accessing domain.tld or domain.tld/, I want to display a custom homepage located at public/home/index.html on the server.
To summerize:
domain.tld or domain.tld/ serves public/home/index.html
domain.tld/something serves public/index/index.html
domain.tld/something/ or domain.tld/some/thing serves public/error/index.html
I already tried using express.static('folder', { redirect: false }) but it doesn't display the index.html file and I can't get the other things to work.
I really don't know how to do it!
I fortunately don't have any code to show you guys as this is more a theorical problem, since I am beggining with expressjs.
Thank you very much in advance for your answers, and please don't hesistate to ask for more details if you need some!

WebpackHTMLPlugin output built references with a prefix

I have a configuration option for my Express app that determines whether to pull static content (JS, CSS, etc.) from a separate URL (e.g. using webpack-dev-server) or serving it "inline" via express.static() in the Express app. So I need to output a different origin depending on that configuration:
<script src="{STATIC_CONTENT_PATH}/[resource reference]"></script>
where {STATIC_CONTENT_PATH} is the web server's origin if serving inline, or the content server's origin if running separately. So far, I've only been able to get it to output a path that is relative to the site root (/publicPath/[resource reference]). Is there an easy way to add a prefix to the path used in the tags that the plugin outputs?
I don't think there's any existing option to do this, so I just used a template, which contains the following:
<%= _.map(htmlWebpackPlugin.files.js, (path) => `<script src="${htmlWebpackPlugin.options.staticContentURL}${path}"></script>` ).join("") %>
(I also passed through the URL as a config option to the plugin)

Mongoose Web Server Config

I'm using the binary version of Mongoose embedded web server (mongoose-free-6.4.exe) to test a small project locally. I noticed that all requests are sent with the following header:
Content-Type: text/plain
Is it possible to force all requests to configure this to UTF-8??
Content-Type: text/html; charset=utf-8
I was looking poking around and do we need to recompile any configuration change ? I was unable to find an easy way to do this. Did I misse something?
I actually found the way to do this on mongoose.conf:
# Add your special mime types here
m *.html=text/html; charset=utf-8
I couldn't find a way to configure the default one on the other hand. But I found this on Git with limited information:
https://github.com/cesanta/fossa/issues/238

Symfony 2 Static asset authorisations (.js behind firewall)

What is the procedure for securing static assets (javascript and css) behind the firewall?
I have an admin section which uses javascript heavily. I don't really want to expose the code to the public.
I currently compile all my javascript using assetic to files in /web/admin/js/xyz.js
Is there a simple way to do this that I'm overlooking?
You could use a controller to serve the static file and secure that controller. Something like:
/**
* Serves static javascript file.
* We have configured /secure to be secured by some firewall
*
* #Route("/secure/xyz.js", name="static_xyz")
*/
public function staticXyzAction()
{
$headers = array(
'Content-Type' => 'text/javascript',
);
return new Response(file_get_contents($this->get('kernel')
->getRootDir().'../web/admin/js/xyz.js'), 200, $headers);
}
This is just an example with the data you provided. Obviously in your final code the file being served should be located in some directory which is not directly accesible by the web server.
The obvious downside to this approach is performance. PHP is much slower for serving an static file than your web server but depending on your load this may not be an issue.
Why do you want to "hide" these admin js files? The js should not perform critical auth or check rights, but just converse with your Sf2 Apis / Controllers which do that, and should not be critical if read. This is a conception matter.
If you are afraid that a lambda user / hacker sees these js files, you could set a very complicated random js output in Assetic. The Symfony .htaccess allows user to access static files only if they know their exact url, they cannot list your repository where you store your builded assets, the firewall catch that.
And last security mesure, use yui-minifier with Assetic to minify and obfusacate your builded js files.

How GWT-platform's Dispatch to indicate my service's endpoint URL in DispathcAsync interface?

Sceneaory:i'm using GWT-platform Dispatch Command pattern.i want to deploy my static content in one http server,such as Appache httpd,and dynamic content in other web container,such as WebSphere etc.
Question: I have a ActionHandler servlet in my backend service,this should be in web container,and which URL will be for example, http://127.0.0.1:8080/services.but my static conten is deployed on http://127.0.0.1:80/Demo/. I don't want to use Appache httpd's proxy-pass,just want to indicate my serviceimpl's url in client's dispatchAsync interface,how i can implement it?
Thanks!
In your Guice module you can configure the URL:
serveRegex("/" + ActionImpl.DEFAULT_SERVICE_NAME + ".*").with(
DispatchServiceImpl.class);
Have a look at the documentation

Resources