Blog Spot!


NodeJS gzip content with Express

How to serve pre-archived asset files.

Provides a small layer on top of serve-static, which allows to serve pre-gzipped files. Supports brotli and allows configuring any other compression you can think of as well.

"Hacky" solution for express applications

Change the asset URL with middleware.

app.get('*.js', function(req, res, next) {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    res.set('Content-Type', 'text/javascript');
    next();
});

app.get('*.css', function(req, res, next) {
    req.url = req.url + '.gz';
    res.set('Content-Encoding', 'gzip');
    res.set('Content-Type', 'text/css');
    next();
});

Apache gzip content and modules setup

Apache Module mod_deflate

This module provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network.

How to enable disable Apache modules.

  • a2enmod|a2dismod MODULE_NAME

Example enable mod_deflate

$ sudo a2enmod mod_deflate

Serving pre-compressed content

Since mod_deflate re-compresses content each time a request is made, some performance benefit can be derived by pre-compressing the content and telling mod_deflate to serve them without re-compressing them. This may be accomplished using a configuration like the following:

<IfModule mod_headers.c>
    # Serve gzip compressed CSS and JS files if they exist
    # and the client accepts gzip.
    RewriteCond "%{HTTP:Accept-encoding}" "gzip"
    RewriteCond "%{REQUEST_FILENAME}\.gz" -s
    RewriteRule "^(.*)\.(css|js)"         "$1\.$2\.gz" [QSA]

    # Serve correct content types, and prevent mod_deflate double gzip.
    RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
    RewriteRule "\.js\.gz$"  "-" [T=text/javascript,E=no-gzip:1]

    <FilesMatch "(\.js\.gz|\.css\.gz)$">
      # Serve correct encoding type.
      Header append Content-Encoding gzip

      # Force proxies to cache gzipped &
      # non-gzipped css/js files separately.
      Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

Manual gzip with htaccess

Setup for pre-compressed content. Compression can be done with: gzip -rk *.js *.css

Than add to .htaccess

RewriteEngine on
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.(js|css)$ $1\.$2\.gz [QSA]

RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1,E=manualgzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1,E=manualgzip:1]

<ifmodule mod_headers.c>
  # setup this header only if rewrites above were used
  Header set Content-Encoding "gzip" env=manualgzip
</ifmodule>

Added on 08.Feb.2022
Tags: content-encoding deflate compress gzip assets serve

Find files modified in The last N Days or Minutes

To find the files that have been changed (with the files data modification time older than) in the last N days from a directory and subdirectories, use:

find /directory/path/ -mtime -N -ls

Where:

  • find is the Unix command line tool for finding files (and more)
  • /directory/path/ is the directory path where to look for files that have been modified. Replace it with the path of the directory where you want to look for files that have been modified in the last N days
  • -mtime -N is used to match files that had their data modified in the last N days. Replace N with a number (integer)
  • -ls lists the resulting files (the files that have been modified in the last N days) in ls -dils format on standard output. You can skip this, but using it you'll get more information like the file size, permissions, the modification date, etc.

Examples:

  • Find all files modified in the last day (24 hours; between now and a day ago) in a directory and subdirectories:

    find /directory/path/ -mtime -1 -ls

    -mtime -1 is the same as -mtime 0.

  • Find all files modified in the last 30 days:

    find /directory/path/ -mtime -30 -ls
  • If you need to find the files that have a modification date older than N, for example older than 30 days? In that case you need to use +N instead of -N, like this:

    find /directory/path/ -mtime +N -ls
  • Find all files with a modification date older than 7 days:
    find /directory/path/ -mtime +7 -ls
  • Find all files modified more than 48 hours ago (at least 2 days ago):

    find /directory/path/ -mtime +1 -ls
  • Find all files modified between 24 and 48 hours ago (between 1 and 2 days ago):
    find /directory/path/ -mtime 1 -ls

So why is 1 one day ago, and +1 older than 2 days / 48 hours ago? That's because according to the man find, any fractional parts are ignored, so if a file was last modified 1 day and 23 hours ago, -mtime +1 won't match it, treating it as if the file was last modified 1 day, 0 hours, 0 minutes, and 0 seconds ago; see this explanation on why that's the case.

This being the case, how can you get all files modified at least 1 day ago? Use +0:

find /directory/path/ -mtime +0 -ls

Using minutes instead of days

To find the files that have been modified N minutes ago, or with a modification date older than N, simply replace -mtime with -mmin.

article | man find

Added on 28.Dec.2021
Tags: find mtime modified file modification time fs

Simple - lightweight wiki with PHP

WikWiki

Minimalist Wiki like script

LionWiki

Minimalist Wiki, but more advanced than 'WikWiki', has two version full & core.

Core version is just one php file, much better implementation than 'WikWiki'. Supports different templates. The full version has some more features, still just 250KB in size.

DokuWiki

DokuWiki is a simple to use and highly versatile Open Source wiki software that doesn't require a database.

Full featured lightweight wiki. No database required!

Added on 20.Dec.2021
Tags: one-file php file wiki script

Neard, Portable WAMP software stack for windows

Neard

Neard is a portable WAMP software stack involving useful binaries, tools and applications for your web development. It is a free and open source software and always will be!

Windows 7 or later.

Added on 18.Dec.2021
Tags: server wamp php node development web

Search


PHP Libraries


Carbon lib / docs
Idiorm lib / docs
Image Workshop lib / docs
lorenzos/Minixed lib / docs
Parsedown lib / docs
PHP Paginator lib / docs
PHP Redis lib / docs
QrCode lib / docs
Requests lib / docs
Slim lib / docs
Spyc lib / docs
TWIG lib / docs
Upload lib / docs
Validation lib / docs
Zebra Image lib / docs

JS Libraries


AJV lib / docs
BackboneJS lib / docs
Bootstrap Notify lib / docs
C3.js lib / docs
ChartJS lib / docs
FastMD5 lib / docs
HighlightJS lib / docs
jQuery-Storage lib / docs
JS-Cookie lib / docs
Leaflet JS lib / docs
LowDB lib / docs
Marked lib / docs
NeedlyJS lib / docs
ParcelJS lib / docs
RequireJS lib / docs
Swig lib / docs
Toastr lib / docs
Underscore lib / docs
ValidateJS lib / docs
top