Handling user-submitted data
We're going to implement the classic guessing game of Hangman (see https://en.wikipedia.org/wiki/Hangman_(game)). Users will be able to post new words to guess, and to guess words posted by others. We'll look at creating new games first.
First, we'll add a new module for managing our games. For now, we'll just store our games in the memory. If we want to put games in some persistent storage in future, this is the module we will change. The interface (that is, the functions added to module.exports
) can remain the same though.
We add the following code under services/games.js
:
'use strict'; const games = []; let nextId = 1; class Game { constructor(id, setBy, word) { this.id = id; this.setBy = setBy; this.word = word.toUpperCase(); } } module.exports.create = (userId, word) => { const newGame = new Game(nextId++, userId, word); games.push(newGame); return newGame; } module.exports.get = (id) => games.find(game...