/NextLevelWeek14

NLW Expert NodeJS - Fastify, Prisma, Websockets

Primary LanguageTypeScript




📊 About

A real-time polling system where users can create a poll, and others can actively participate by submitting their votes. The platform generates an instant ranking based on the choices, continuously updating the vote counts.

📚 Technologies e libs

💻 Routes

POST /polls

Run in Hoppscotch

Create a new poll.

Request body

{
  "title": "What is the best programming language?",
  "options": [
    "JavaScript",
    "Java",
    "PHP",
    "C#"
  ]
}

Response body

{
  "pollId": "194cef63-2ccf-46a3-aad1-aa94b2bc89b0"
}

GET /polls/:pollId

Run in Hoppscotch

Return data from a single poll.

Response body

{
	"poll": {
		"id": "e4365599-0205-4429-9808-ea1f94062a5f",
		"title": "Qual a melhor linguagem de programação?",
		"options": [
			{
				"id": "4af3fca1-91dc-4c2d-b6aa-897ad5042c84",
				"title": "JavaScript",
				"score": 1
			},
			{
				"id": "780b8e25-a40e-4301-ab32-77ebf8c79da8",
				"title": "Java",
				"score": 0
			},
			{
				"id": "539fa272-152b-478f-9f53-8472cddb7491",
				"title": "PHP",
				"score": 0
			},
			{
				"id": "ca1d4af3-347a-4d77-b08b-528b181fe80e",
				"title": "C#",
				"score": 0
			}
		]
	}
}

POST /polls/:pollId/votes

Run in Hoppscotch

Add a vote to specific poll.

Request body

{
  "pollOptionId": "31cca9dc-15da-44d4-ad7f-12b86610fe98"
}

WebSockets ws /polls/:pollId/results

{
  "pollOptionId": "31cca9dc-15da-44d4-ad7f-12b86610fe98"
}

Message

{
  "pollOptionId": "da9601cc-0b58-4395-8865-113cbdc42089",
  "votes": 2
}

Run in wscat:

$ npm i -g wscat

wscat -c ws://localhost:3333/polls/:pollId/results

📗 Swagger Documentation

📝 How to run the project

#  Clone this repository.
$ git clone https://github.com/KRochaS/NextLevelWeek14.git

# Navigate to the project folder in the terminal/cmd.
$ cd NextLevelWeek14/

# Install the dependencies.
$ npm i ou yarn install

# Setup PostgreSQL and Redis
$ docker compose up -d

# Copy .env.example to .env file

# run the project
$ npm run dev

# HTTP server running on http://localhost:3333