DEV Community

Falah Al Fitri
Falah Al Fitri

Posted on

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 :)

Top comments (0)