As my per understanding, you want to combine deferent pages.
You need to develop a function that read deferent files and combine them together and send the response to the client. You need to define the content type to 'text/html'.
Sample Functions:
Description:
1. Read the main page
2. Read header
3. Read footer
4. Combine the page part
5. Send to the response.
// Main Object
_index = {};
// This object return content of the page
_index.get = function(data, callback) {
const templateData = {};
templateData['head.title'] = 'Page Title';
templateData['head.description'] = 'Description';
templateData['body.title'] = 'Body Title';
templateData['body.class'] = 'css-class';
// Read the main page contant.
_index.getTemplate('index', templateData, (err, str) => {
if (!err && str) {
// Read rest of the page contant and put them together.
_index.addUniversalTemplate(str, templateData, (err, str) => {
if ((!err, str)) {
callback(200, str, 'html');
} else {
callback(500, undefined, 'html');
}
});
} else {
callback(500, undefined, 'html');
}
});
// callback(undefined, undefined, 'html');
};
_index.getTemplate = (templateName, data, callback) => {
templateName =
typeof templateName == 'string' && templateName.length > 0
? templateName
: false;
if (templateName) {
const templateDir = path.join(__dirname, './../template/');
fs.readFile(templateDir + templateName + '.html', 'utf8', (err, str) => {
if (!err && str && str.length > 0) {
// Do interpolation on the data
let finalString = _index.interpolate(str, data);
callback(false, finalString);
} else {
callback('No Template could be found.');
}
});
} else {
callback('A valid template name was not specified.');
}
};
// Add the universal header and footer to a string and pass provided data object to the header and footer for interpolation.
_index.addUniversalTemplate = function(str, data, callback) {
str = typeof str == 'string' && str.length > 0 ? str : '';
data = typeof data == 'object' && data !== null ? data : {};
// Get header
_index.getTemplate('_header', data, (err, headerString) => {
if (!err && headerString) {
_index.getTemplate('_footer', data, (err, footerTemplate) => {
if (!err && footerTemplate) {
let fullString = headerString + str + footerTemplate;
callback(false, fullString);
} else {
callback('Could not find the footer template');
}
});
} else {
callback('Could not find the header template.');
}
});
};