I have been asked to perform following task

  1. Take a code input from frontend, i.e user would give his code on frontend (design for a web/landing page)

  2. On backend we have many fields inside an api route

    route.get("/", (req, res) => {
        const fullName: "Varun Bindal"
        const contactNo = 9293939933
       const message = "Message I want to display"
     //Many more 
    }
    
  3. Tell user a way where when we serve his code such that, he could dynamically access/assign the fields we have in the backend into his code

I did some googling and found the express officially recommends ejs for server side compilation of webpage

Can someone please help me figure out how we can achieve this?

1 Answers

3
ne1410s On

Yes you can! Firstly you must include ejs in your project, configure it in your server.js file for example, then you can call res.render() in your callback parameter on route.get().

In your html or javascript you can create a placeholder which gets populated.

Example (server):

route.engine('html', ejs.renderFile);
route.engine('js', ejs.renderFile);

route.get('/', (req, res) => res.render(path.resolve(__dirname, ' 
   ../ui/index.html'), {
    'myVal': 42,
}));

Example (client html, js, etc...):

<%= myVal %>