Recently I started working with Node.js, but I encountered an error when trying to run a program. The error message says "the term nodemon is not recognized the name of cmdlet, function, script file or operable function". Can someone please assist me with this?
index.js
// This is a Node server which will handle socket io
// If a new user is joined then socket.io fires
const io = require('socket.io')(7000)
const users = {};
io.on('connection', socket => {
socket.on('new-user-joined', name => {
users[socket.id] = name;
socket.broadcast.emit('user-joined', name)
});
socket.on('send', message => {
socket.broadcast.emit('receive', { message: message, name: users[socket.id] })
});
socket.on('disconnect', message => {
socket.broadcast.emit('left', users[socket.id]);
delete users[socket.id];
});
});
client.js
This is my client file where I define all the tasks like where messages should be sent and received.
const socket = io('http://localhost:7000');
const form = document.getElementById('send-container');
const messageInput = document.getElementById('messageInp');
const messageContainer = document.querySelector('.container');
var audio = new Audio('whistle.mp3');
const append = (message, position) => {
const messageElement = document.createElement('div')
messageElement.innerText = message;
messageElement.classList.add('message');
messageElement.classList.add(position);
messageContainer.append(messageElement);
if(position =='left') {
audio.play();
}
}
const name = prompt("Enter Your Name to join");
socket.emit('new-user-joined', name)
socket.on('user-joined', name => {
append(`${name} joined the chat`, 'right');
})
socket.on('receive', data => {
append(`${data.name}: ${data.message}`, 'left');
})
socket.on('left', name => {
append(`${name}: left the chat`, 'right');
})
form.addEventListener('submit', (e) => {
e.preventDefault();
const message = messageInput.value;
append(`You: ${message}`, 'right');
socket.emit('send', message);
messageInput.value = '';
})