Convert a URL to a valid and safe filename in NodeJS

Written by code-diesel - - Aggregated on Wednesday May 16, 2018
Tags: nodejs, files

A recent web scraping project necessitated creating hundreds of files to store scraped web page contents. The question was how best to name the files for a particular url.

Initially I thought of using the shortid npm package. However, the clients requirement was that he should be able to see easily which web page was parsed in which file. This entailed using the url itself as a source for the filename. After searching for a short while I found the filenamify-url npm package. This allows one to create a valid – unix or Windows style – filename from a url. Details on how to use the package is given below.

Installation is as usual using npm.

npm install --save filenamify-url

Usage is simple enough.

var filenamifyUrl = require('filenamify-url');
valid_file = filenamifyUrl('');

The url protocol strings are removed and any invalid characters not acceptable in a filename are replaced with the ‘!’ character. You can however specify a replacement character for the default ‘!’ character.

valid_file = filenamifyUrl('', {replacement: '#'});

Note: On Unix-like systems the character / is reserved and :”/\|?* on Windows.

Once we get a valid filename string, we can prefix or suffix additional information depending on ones requirement – like adding a timestamp and a extension.

valid_file = filenamifyUrl(URL);
final_filename = valid_file + "-" + + ".txt";


The post Convert a URL to a valid and safe filename in NodeJS appeared first on codediesel.

« How much have I spent with Amazon? - Eric L. Barnes

Laracasts - Workshop - Contact Support Modal »