-3

I have three pages: server.js, user.js, index.js. my server takes data from the request header and builds a User object. the user object has a method that does calculate and save at result. I want to pass the result to index.js or to an HTML page and manipulate the result. I try to save the result in a static verb and create obj in the index.page. but I get Error. any idea how to do that?

    //server.js 

    const User = require('./user.js');
    var express = require('express');
    var app = express();
    app.get('/', function(req, res) {
        res.sendFile(__dirname + '/index.html');
        console.log("connecting to server");
        jsonHeader = JSON.stringify(req.headers);
        user = CreateUserObject(req);
        User.setResult(30); }



    // user.js


class User{
    constructor(){
        this.ip=0;
        this.language =' ';
        this.result = "";
    }

    setUserDetails(requestHeader){
        this.Ip = requestHeader.header('x-forwarded-for');
        this.language = requestHeader.header('accept-language');
    }
    setResult(number){
        User.staticResult = number;

    }
    getResult(){
        return User.staticResult;
    }

};
User.staticResult = 15;
module.exports = User;



//index.js
import User from 'user.js';
var user  = new User();
 ................
aviv.L
  • 39
  • 9

1 Answers1

0

I guess your problem is to pass data to the HTML file.

If yes, then this can be solved using the template engine.

handlebars is one of them, I find it easy to use

app.js

const handleBars = require('handlebars');
readFile("fileName.html,'utf8').then(
 function(data)
 {
    let template = handleBars.compile(data,{strict:true});
    let replacements = /*assign the value to html file variable enclosed in {{ }}*/
    {
       title : "MyTitle",
       body  : "MyBody"
    }
    let html = template(replacements);
    let fss = require('fs');
    fss.writeFile("test.html", html, function(err) {
    if(err) 
    {
     return console.log(err);
    }
  }

filename.html

<div class="entry">
  <h1>{{title}}</h1>
   <div class="body">
     {{body}}
   </div>
</div>

For more information how to use handlebars