0

To improve the performance of the application , I want to host my static assets on CDN. But we have not yet created a domain using nginx. How to still use a cdn like - express-simple-cdn link -

https://www.keycdn.com/support/express-cdn-integration

i need to write this domainName.com in my app.js file of Angular app through which i host my application--

// Initialize Express in a variable in app.js
var app = express();

// Define Your CDN base path
var CDN = "https://assets.preisheld.ch";
// Register a local variable in your app which contains the CDN function
app.locals.CDN = function(path, type, classes, alt) {
    if(type == 'js') {
        return "<script src='"+CDN+path+"' type='text/javascript'></script>";
    } else if (type == 'css') {
        return "<link rel='stylesheet' type='text/css' href='"+CDN+path+"'/>";
    } else if (type == 'img') {
        return "<img class='"+classes+"' src='"+CDN+path+"' alt='"+alt+"' />";
    } else {
        return "";
    }
};

How to utilize CDN in such a situation ?

Techdive
  • 997
  • 3
  • 24
  • 49
  • 1
    Can you elaborate a bit on what you want to do? What do you mean when you say you need a CDN locally? That doesn't make much sense. Sounds like you just need to run a web server locally, and set a hostname. (You can edit your `hosts` file and use whatever hostname you want.) – Brad Sep 24 '19 at 05:30
  • I have images in my assets which i want to host in the form of cdn. How to include that in the code ---> app.locals.CDN = function(path) { return CDN(path, '//yourzone-99ff.kxcdn.com') }; – Techdive Sep 24 '19 at 05:54
  • As here a .com is used – Techdive Sep 24 '19 at 05:54
  • I don't think you mean that you actually want to use a CDN. I think you mean that you simply want to use a hostname, correct? – Brad Sep 24 '19 at 06:01
  • My ultimate aim is to create a CD for which a hostname is required – Techdive Sep 24 '19 at 06:08
  • *CDN - correction for CD – Techdive Sep 24 '19 at 06:17
  • A CDN is a distributed network of servers spread out geographically so that content can be accessed much faster and more efficiently than having to go across the whole internet to get it. What you seem to be asking for is to run a CDN on localhost, which makes absolutely zero sense. Can you please try again to elaborate on what you need to do? Are you sure you don't just need to configure your `hosts` file? – Brad Sep 24 '19 at 06:22
  • Hi Brad. I am unaware of what is a host file ? is it the file from where we host our server .i.e app-server.js in angular or index.html file of the application or is it a file where you define your domainName.com ? I will configure my host file if it is app-server.js with the definition as edited in the question. However, I find that the domainName.com needs to be mentioned for a CDN . But i don't have a domain name yet. We open our application on ipaddress:port . So is there a dummy a place where you can create your domainName.com address for free while opening it in your local ip address – Techdive Sep 24 '19 at 06:35

1 Answers1

1

So is there a dummy a place where you can create your domainName.com address for free while opening it in your local ip address

Yes! That's the hosts file. You can find it on your system here:

  • Windows: C:\Windows\System32\Drivers\etc\hosts
  • Linux: /etc/hosts
  • OS X: /private/etc/hosts

This file allows you to map arbitrary host names to network addresses, overriding anything that comes from DNS. For example:

127.0.0.1 yourproject.test

Now, if you go to http://yourproject.test in your browser, it will resolve to 127.0.0.1 and connect the web server running on your loopback address. If you're using IPv6, use ::1 instead of 127.0.0.1.

You can set any hostnames you want... even real ones that exist. However, it is strongly recommended that you use the .test top-level domain, as it's specifically reserved for testing purposes.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thanks brad ! But what about the port. I host my application in the browser with 192.xxx.x.xx0 :4200 . How will I expose the port ? Is it ok that I run my application on 192.xxx.x.xx0 :4200 and for the CDN i define it in the hosts file as -> 192.xxx.x.xxx projects.test ? – Techdive Sep 24 '19 at 06:53
  • Hi Brad, can you help me with this - https://stackoverflow.com/questions/58079941/performance-has-improved-by-using-express-simple-cdn-but-get-404-not-found-error – Techdive Sep 24 '19 at 13:02
  • @Techdive The `hosts` file is only for editing the hostname. If you're using a particular port, you'd put that in the URL like any other hostname. For example, `http://yourproject.test:4200`. – Brad Sep 24 '19 at 16:13