Hello i am beginner in JavaScript. I am trying to access the functions of user.js in script.js but it is throwing error i have attached ss also, and below is the implementation of what I am doing.
I have create 4 files
index.js (starting file):
const express = require("express");
const app = express();
const path = require("path");
const { getUserName, getRoomUsers } = require("./utils/user");
const port = process.env.PORT || 3030;
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use("/home", express.static("public"));
app.get("/home/welcome", (req, res) => {
res.render("home");
});
app.get("/", (req, res) => {
res.send("Welcomme to startup page!!");
});
app.listen(port);
home.ejs (in /views):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>welcome to home page</h1>
<script src="script.js"></script>
</body>
</html>
user.js (in /utils):
let user = [];
function getUserName(id, room) {
const obj = users.find((user) => user.room === room && user.id === id);
return obj.username;
}
function getRoomUsers(room) {
return users.filter((user) => user.room === room);
}
function userJoin(id, username, room) {
const user = { id, username, room };
users.push(user);
return user;
}
module.exports = {
userJoin,
getRoomUsers,
getUserName,
};
script.js (in /public):
const { getUserName, getRoomUsers } = require('./../utils/user.js')
getRoomUsers("abcd")
on running this i am getting the following error
how can i access the user.js functions in script.js