Node.js get file extension

I believe you can do the following to get the extension of a file name.

var path = require('path')

path.extname('index.html')
// returns
'.html'

If you would like to get all extensions in a file name (e.g. filename.css.gz => css.gz), try this:

const ext="filename.css.gz"
  .split('.')
  .filter(Boolean) // removes empty extensions (e.g. `filename...txt`)
  .slice(1)
  .join('.')

console.log(ext) // prints 'css.gz'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)