Search code examples
node.jsstaticejsbundlefastify

how to use same identifier for different directory paths in EJS with different static assets folder in Nodejs web app


Issue

this is my ejs

<head>
   
     <%- include('../../public/components/headercontent.html'); %>
     
</head>

and below is my app/server.js file configuration

fastify.register(require("point-of-view"), {
  engine: {
    ejs: require("ejs"),
  },
  root: path.join(__dirname, "../views"),
});


fastify.register(require("fastify-static"), {
  root: path.join(__dirname, "../") + "/public/",
});

so the above works as expected my problem statement is below i have two static assets folder

  • public
  • public-release

what is expected scenario is

I want to use same public name but path can be different something like
case scenario 1

fastify.register(require("fastify-static"), {
  root: path.join(__dirname, "../") + "/**public-release**/",
  nameused:'public'
});

case scenario 2

fastify.register(require("fastify-static"), {
  root: path.join(__dirname, "../") + "/**public**/",
  nameused:'public'
});

is there a way I can achieve this ?

Objective :

1.i want to switch dynamically my js and css for home grown release /bundling system and without touching my existing codebase of linking them

2.so the idea here is to just the update the point where static assests are served and change them through custom yarn or npm run scripts

  1. for eg:- when i pass say dev the static assets should serve from 'public ' folder with 'public' as alias

    and when i pass say prod the static assets should serve from 'public-release' folder with 'public' as alias


Solution

  • So , there is middle ground approach which I implemented for my boilerplate

    Github Repo: Node.js Postgresql fastify

    TLDR;

    Node.js web APP Release Mechanism pt-1

    Node.js web APP Release Mechanism pt-2

    How to Release Node.js Web APP