我有三个页面:server.js、user.js、index.js。我的服务器从请求标头中获取数据并构建一个用户对象。用户对象有一个计算并保存结果的方法。我想将结果传递给 index.js 或 HTML 页面并操作结果。我尝试将结果保存在静态动词中并在 index.page 中创建 obj。但我得到错误。知道怎么做吗?
//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();
................