


In the daily development process, we often need to modify some static files (such as JavaScript, CSS, HTML files, etc.) placed on the CDN. In this process, we hope to have a way to map the online CDN directory to local A directory on the hard disk, so that when we modify a file locally, there is no need to publish it, and the effect can be seen immediately after refreshing.
For example, our CDN domain name is: http://a.mycdn.com, and the corresponding local directory is: D:\workassets. We hope that all requests to http://a.mycdn.com/* Access is mapped to the local D:\workassets\*. For example, when accessing http://a.mycdn.com/s/atp.js, the local D:\workassetss\atp.js is actually read, without downloading online files from the Internet.
It is very simple to implement this function. The key points are as follows:
1. Open an HTTP service locally and listen to port 80;
2. Modify the system hosts file and add "127.0.0.1 a.mycdn.com", bind the CDN domain name to the local server address;
3. Configure the local HTTP service. After receiving a GET request, first check whether the corresponding file exists on the local hard disk. If it exists, return If the content of this file does not exist, the corresponding content online will be returned.
As you can see, the key part is to build a local HTTP service. There are many tutorials in this area, such as installing server software such as Apache or Ngnix locally, and then configuring corresponding forwarding rules. However, I personally feel that this method is still a bit complicated. What this article will introduce is another method that does not require the installation of server software.
Because we are developing and debugging locally, the requirements for performance and concurrency are not high, so we actually do not need a professional HTTP software like Apache/Ngnix, we only need a section that can provide HTTP Just use the service script. For example, use nodejs to implement it.
Copy code The code is as follows:
/**
* author: oldj
*
**/
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
local_folders,
base_url;
local_folders = [ // Local path, the agent will search for files in the directories in this list, and if not found, go to the online address
"D:/work/assets"
];
base_url = "http://10.232.133.214"; // Online path, if the file cannot be found, redirect to this address
function loadFile(pathname, response) {
var i, l = local_folders.length,
fn;
console.log("try to load " + pathname);
for (i = 0; i
fn = local_folders[i] + pathname;
if (path.existsSync(fn) && fs.statSync(fn).isFile()) {
fs.readFile(fn, function (err, data) {
response.writeHead(200);
response.write(data);
response.end();
});
return;
}
}
response.writeHead(302, {
"Location":base_url + pathname
});
response.end();
}
http.createServer(
function (request, response) {
var req_url = request.url,
pathname;
// Processing Request similar to http://www.php.cn/,tbsp/tbsp.css?t=20110920172000.css
pathname = req_url.indexOf("??") == -1 ? url.parse(request. url).pathname : req_url;
console.log("Request for '" + pathname + "' received.");
loadFile(pathname, response);
}).listen( 80);
Please change the values of the local_folders and base_url variables above to the values you need. Save this file, for example, as local-cdn-proxy.js, and then execute "node local-cdn-proxy.js" on the command line. The local server will be running. Of course, don't forget to bind hosts.
When accessing a path through http, the above script will first search in the corresponding local directory. If found, it will return the contents of the corresponding file. If it is not found, it will directly 302 jump to the corresponding online address. For situations where it cannot be found, another solution is to have the local server download the corresponding content from online and return it, but for this requirement, a 302 jump is enough.
In addition to the nodejs version, I also wrote a Python version:
Copy code The code is as follows:
# -*- coding: utf-8 -*-
## author: oldj
#import os
import BaseHTTPServer
LOCAL_FOLDERS = [
"D:/work/assets"
]
BASE_URL = "http://10.232.133.214"
class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
print "Request for '%s' received." % self.path
for folder in LOCAL_FOLDERS:
fn = os.path.join(folder, self. path.replace("/", os.sep)[1:])
If os.path.isfile(fn):
self.send_response(200)
self.wfile.write(open( fn, "rb").read())
’s (BASE_URL, self.path))
server = BaseHTTPServer.HTTPServer(("0.0.0.0", 80), WebRequestHandler)
server.serve_forever()
As you can see, the Python version of the code is much more streamlined than the nodejs version.
For more related articles about a simple HTTP static file server written in nodejs and Python, please pay attention to the PHP Chinese website!

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
