Using a CDN with expressJS - express

I start to play with ExpressJS for an app. I use the app.use(express.static(__dirname + '/public')); line to configure access to the public folder.
But as I use a CDN, I would like to point to the public folder who will contain JS, CSS & img files. This is an example :
http://cdn.com/public/css/style.css
Is anyone who can help me to fix the issue ?
Thanks

"Use a CDN" means "load files from the CDN's servers instead of your own app server". Thus when you use a CDN, your app server does not handle those files. You just need to change your URLs in your HTML to point to the CDN.

Actually, CDN provider like MAXCdn are waiting for a readable folder. So, I'm looking for the express paramater that replace __dirname.
So I did a simple thing : add a repository nested. In this way, I get http://domain.tld/public/img/someImage.jpg way.

You could use express-simple-cdn node module and then in Jade template use CDN() function:
link(rel="stylesheet", href=CDN('/css/style.css'))
It would output:
<link rel="stylesheet" href="http://cdn.com/public/css/style.css">

Related

VueJS: Reference static file from public/index.html

I am hardcoding meta og tags into public/index.html as I'm making a single page app. I can't find a way to reference the image from the assets though.
Is it possible to achieve this ? Otherwise I'll just upload it to strapi and have the backend server serve the file.
The next step is to use Nuxt.js to have the website as static files so I'm not really eager to use back-end server to serve the image.

How to change base url?

I am not sure the title of the question is correct, but I'll try to explain what I need.
We host multiple web applications on a single machine, so
https://localhost:8080
and
https://localhost:8081
point to different applications.
Meanwhile, API gateway maps request without dropping the URL suffix:
https://api.domain.com/service1/Home/Index
turns into
https://localhost:8080/service1/Home/Index
and
https://api.domain.com/service2/Home/Index
into
https://localhost:8081/service2/Home/Index
I would like the app's root ~ to resolve to hostUrl+suffix where suffix is a configured value.
I used this blogpost to globally prefix all the routes for controllers and pages, but now I struggle with static files.
I am able to virtually move wwwroot:
app.UseStaticFiles(new StaticFileOptions
{
RequestPath = $"/{GlobalPrefix}"
});
but <link href="~/bootstrap/css/bootstrap.css" rel="stylesheet" /> does not contain GlobalPrefix part when rendered.
So I would like to add this GlobalPrefix to whatever base URL site is hosted at. Be it a self-hosted app or in IIS.
P.S. RTFM =)
Using app.UsePathBase("/myPath")(https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.usepathbaseextensions.usepathbase?view=aspnetcore-2.2) is working in that case.
There have been changes in ASP.NET Core 2 regarding that (http://github.com/aspnet/Announcements/issues/226). Also, be aware of a strange behavior: http://github.com/aspnet/HttpAbstractions/issues/893

Prevent access to public webpack bundle? ExpressJS

In my webpack config I have the publicPath set like so:
publicPath: '/js'
This way it points to public/js. Also in my index.pug file, which is loaded by the server and not in the public folder I have this:
extends layout
block content
main#app
script(src="/js/bundle.js")
Unfortunately, this enables people accessing my site to visit example.com/js/bundle.js. Is there a way to prevent this?
If /js/bundle.js is a script file you are using in your web page, then there is NO way to prevent the browser from going directly to http://example.com/js/bundle.js. That's the exact URL that the browser uses to load the script from your web page so that URL has to work.
ALL Javascript that runs in your web page is openly available to the public. You cannot change that. That's the architecture of the web and browsers.
Unfortunately, this enables people accessing my site to visit example.com/js/bundle.js. Is there a way to prevent this?
No. You cannot prevent it.

Aurelia with out a router, navigate to login.html and not /#login

I'm writing custom views for Identity Server 3 for its login, logout, consent, etc. I want to use Aurelia but in Identity server I have to pass in the actual html file. So, how can I navigate to localhost:9000/login.html and not localhost:9000/#login
Thanks
if you want to remove the # from URLs in an aurelia application:
Configuring PushState
Remember that Aurelia is "just JavaScript," so when you want to link to another page on your site, you'll do it the same way you always would, by giving either an absolute or relative path to the file. You're not going to be able to link to an HTML file that is in your bundle though, the file will have to be unbundled, like index.html is.
At this point, this becomes just a standard JavaScript question about building a URL, and not anything to do with Aurelia. So just have the url point to wherever login.html is. I'm hoping this is enough for the answer, if not we can discuss it further.
With #Ashley Grant's help, I was able to create an aurelia replacement for IViewService example coded in angular. Identity Server 3 IView Service Example
It's honestly quite easy. Just create a standard aurelia project and put your html in app.html and view model in app.js. In addition to having the vendor_bundle reference link in your Index.html you will also have to create a link for the app_bundle as well because of a bug that tries to locate app_bundle in the root of your folder structure for Identity Server.
If anyone is interested in the source I can put something together just IM me.

2 dojo base urls

I'm using dojo hosted on google's CDN, which means google's cdn is dojo's base url path. But I have files on my server I want to load with dojo.require. how can I do this? currently I'm getting an error that it can't access the file, but that's because it doesn't exist on google's cdn.
You can config at data-dojo-config or in djConfig
By setting baseUrl and modulePaths
This link would be help. http://dojotoolkit.org/documentation/tutorials/1.6/cdn/