I’m working on a web app that uses socket.io for client to server communication.
When I run the site, in the inspect tab it gives me Uncaught ReferenceError: io is not defined at script.js:1 (anonymous) @ script.js:1
Here is my HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hack.top - Sign Up</title>
<link href="../style.css" rel="stylesheet" type="text/css">
</head>
<body>
<nav>
<a href="/" class="first">
<div class='title-container'><h3>Hack.top</h3><h3 id='title-underscore'>_</h3></div>
</a>
<div class="links">
<a href="">learn more</a>
<a href="">competitions</a>
<a href="">contact</a>
<!-- <a href="/signup">
<button>sign up</button>
</a> -->
</div>
</nav>
<main>
<div class="info signup">
<h3>Hello!</h3>
<p>Sign up to start hacking.</p>
<input type="email" placeholder="[email protected]" id="email">
<input type="text" placeholder="Username" id="username">
<input type="password" placeholder="Password" id="password">
<input type="password" placeholder="Confirm password" id="confirmPassword">
<p>Already have an account? <a href="/login">log in</a></p>
<button id="signUpButton">sign up</button>
</div>
</main>
<script src="../script.js"></script>
<script src="/socket.io/socket.io.js"></script>
</body>
</html>
And my client-side JS:
socket = io();
socket.on('logged', function (res){
if(res.err){
//res.message...
} else {
}
});
document.getElementById("signUpButton").addEventListener("click", createAccount);
function login(){
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const user = {
'username': username,
'password': password
}
socket.emit('login', user);
}
function createAccount(){
const email = document.getElementById('email').value;
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;
if(password != confirmPassword){
//alert user
}
const user = {
'username': username,
'password': password,
'email': email
}
socket.emit('createAccount', user);
}
I don’t think server side JS is necessary for this issue but if someone needs to see just ask and I’ll add it.
Not sure why this is happening, thanks.