NodeJS http homepage 30: create custom module and export

antelove19

Falah Al Fitri

Posted on December 20, 2019

NodeJS http homepage 30: create custom module and export

Happy Coding

previous post


DRY is Don't Repeat Yourself, which will be used in link menu.

We will create custom module and export it.

create a file: public/partials/menu.js and write code such as:

var link =  `
                <ul>
                    <li><a href="/" >Home</a></li>
                    <li><a href="/about" >About</a></li>
                </ul>
            `;

module.exports = link
Enter fullscreen mode Exit fullscreen mode

In index.js, add require menu file

const menu  = require('../public/partials/menu');
Enter fullscreen mode Exit fullscreen mode

for each

res.write( `<ul>
                <li><a href="/" >Home</a></li>
                <li><a href="/about" >About</a></li>
            </ul>` 
          );
Enter fullscreen mode Exit fullscreen mode

replace with

res.write( menu );
Enter fullscreen mode Exit fullscreen mode

Thank for reading :)

💖 💪 🙅 🚩
antelove19
Falah Al Fitri

Posted on December 20, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related