Migrate express.static options
Express 5 changes several express.static options:
- The
dotfilesoption now defaults to"ignore"(Express 4 served dotfiles by default). Files inside a directory that starts with a dot (.), such as.well-known, will no longer be accessible and will return a 404 Not Found error. - The
hiddenoption is removed and replaced bydotfiles. - The
fromoption (an undocumented alias forroot) is removed and replaced byroot.
This codemod updates express.static() calls to preserve the Express 4 behavior:
- Adds an explicit
dotfiles: 'allow'option to calls that don't already specify adotfiles(orhidden) option. - Renames
hiddentodotfiles(hidden: true→dotfiles: 'allow',hidden: false→dotfiles: 'ignore'). - Renames
fromtoroot.
Example
diff
With existing options
diff
Removed hidden option
diff
Removed from option
diff
Security Consideration
After running this codemod, review each express.static() call to determine if serving dotfiles is actually necessary for your application. If you don't need to serve dotfiles, you can:
- Remove the
dotfiles: 'allow'option to use the new Express 5 default ("ignore") - Or explicitly set
dotfiles: 'deny'to return a 403 Forbidden for dotfile requests
For directories like .well-known that need to be served (e.g., for Android App Links or Apple Universal Links), consider serving them explicitly:
javascript