Created
April 10, 2020 20:49
-
-
Save jensen/21f7c974802a9a99c4673c820b67ffcb to your computer and use it in GitHub Desktop.
Example of endpoints for questions and answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const bodyparser = require("body-parser"); | |
const conversations = [ | |
{ | |
id: "1e14ace4-7807-42a0-b542-9b99798526d2", | |
conversation: ["Hey man", "Hey whats up?", "Nothing much", "Oh thats cool"], | |
questions: [ | |
{ | |
id: "ad97da94-09aa-4125-a377-42b7d0fbc939", | |
question: "What was the first thing said?", | |
answers: ["hey man", "hi", "hello", "heyooo"], | |
}, | |
{ | |
id: "4e0d2a63-baa6-4a2a-b222-20ea038f5b32", | |
question: "What was the second thing said?", | |
answers: [ | |
"hey whats up?", | |
"how are you?", | |
"what is that?", | |
"where am i?", | |
], | |
}, | |
{ | |
id: "0bb2ee33-10ee-4ca7-a6fc-2cedc6fef9e0", | |
question: "What was the third thing said?", | |
answers: [ | |
"nothing much", | |
"i ate pizza", | |
"i drank water", | |
"i drank tea", | |
], | |
}, | |
{ | |
id: "d472b692-d6e9-4011-9e61-16d3f368600f", | |
question: "What was the fourth thing said?", | |
answers: [ | |
"oh thats cool", | |
"oh i dont like that", | |
"that tastes good", | |
"that tastes bad", | |
], | |
}, | |
], | |
}, | |
]; | |
app.use(bodyparser.json()); | |
app.post("/answer", (request, response) => { | |
const conversationId = request.body.conversationId; | |
const questionId = request.body.questionId; | |
const answer = request.body.answer; | |
const conversationIndex = conversations.findIndex( | |
(conversation) => conversation.id === conversationId | |
); | |
const questions = conversations[conversationIndex].questions; | |
const questionIndex = questions.findIndex( | |
(question) => question.id === questionId | |
); | |
response.json({ | |
correct: | |
conversations[conversationIndex].questions[questionIndex].answers[0] === | |
answer, | |
}); | |
}); | |
app.get("/conversation", (request, response) => { | |
response.json(conversations[0]); | |
}); | |
app.listen(3000, () => console.log("Connected to port 3000")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment