WFH not WCS

Science teaches that we only really REMEMBER something once. Being in the moment is the experience, and the first time you recollect the moment, it is unique. Thereafter, we are only recalling the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to implement CDN with Node.js and OpenResty

What is a CDN ?

CDN is abbreviation of Content Delivery Network which is the network that serves contents from locations that is closer to requests source location geographically.

In simple words, your website serves the content that stored in CDN storage to represents that to the users requests instead of read the contents from internal storage. it makes the website faster.

Bellow picture figures out how CDN helps to the server to reduce resources usage.

Based on CDN definition, our example nothing more than the storage management!
CDN represents as a SaaS in the real world, so we use JWT authentication to managing user authentication.

In this example we focused on store CSS and JS files, not multimedia types, but you can use it for the other types as well.

The project included following APIs

Health:

/check

Accept HTTP GET request and responsible to running acknowledgement. we will describe it in section two in this article.

Signup:

/api/v1/users/signup

Accept HTTP POST request and responsible to create new user/client.

Signin:

/api/v1/users/signin

Accept HTTP POST request and responsible to login user/client.

Signout:

/api/v1/users/signup

Accept HTTP POST request and responsible to logout the user/client.

File Upload:

/api/v1/files/upload

Accept HTTP POST request and responsible to upload a new file.

Selected file should be passed in request body as binary type and set the file name in header file-name

Get Files:

/api/v1/files

Accept HTTP GET request and responsible to list user’s/client’s files.

So far we have seen how to project’s backend made. Now we see how to server configured but before that we need to make sure that bellow tools are installed on the server.

Now open the server directory and see docker-cmpose.yml content
as you see, both of CDN and database defined as service in config file, so it’s enough that run the command docker-compose up -d in root path to setup and run whole of project.

After that we need to configure the server
install the OpenResty by following instruction, then configure the server same as nginx.conf file or replace it to original one.

As mentioned in the early of article the health check endpoint defined in the server configuration directly.

But why OpenResty?

By default, Nginx doesn’t provide a way to program logical operators or write custom logic. OpenResty powered by Lua language in order to implementing the logics.

So right now you should reload the Http server

Congratulation your CDN system up and configured successfully.

Now call the routes and see the result.

Add a comment

Related posts:

Os principais mitos do desenvolvimento de software

Os mitos de software são "falsas verdades" que existem no mundo da indústria de software. Engenheiros jovens e pessoas mais experientes tendem a acreditar neles, distorcendo a verdadeira face do…

Are Limiting Beliefs Sabotaging Your Relationships?

Our personal beliefs play a huge role in how we view ourselves, and the world around us — though our beliefs are not reality itself, but rather our thoughts about reality. In our quest for love, many…

Ethereal Apes Genesis

Ethereal Apes are a 10,000 piece derivative NFT collection that pays homage to historical collections on the Ethereum blockchain. Ethereal Apes was inspired by a desire to see the NFT community raise…