A Node.js service that scrapes stories from Hacker News, stores them in a MySQL database, and provides REST API endpoints and WebSocket updates for accessing the data.
-
Clone the Repository:
git clone https://github.com/nitish-singh07/backendAssignment.git cd backendAssignment -
Install Dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile in the root directory with the following variables:DB_HOST=<your-database-host> DB_USER=<your-database-username> DB_PASSWORD=<your-database-password> DB_NAME=<your-database-name> PORT=3000 WS_PORT=8080
-
Initialize the Database: Run the SQL script provided in the
scriptsfolder to set up thestoriestable. -
Start the Application:
npm start
- Endpoint:
GET /api/stories - Response:
{ "stories": [ { "title": "Story Title", "url": "https://story-url.com", "author": "Author Name", "score": 123, "time_published": "2025-01-25T12:00:00.000Z" } ] }
- Endpoint:
GET /api/stories/count - Response:
{ "count": 10, "stories": [ { "title": "Story Title", "url": "https://story-url.com", "author": "Author Name", "score": 123, "time_published": "2025-01-25T12:00:00.000Z" } ] }
-
Connect to the WebSocket server:
ws://localhost:8080 -
On connection, receive:
- Initial Count: Number of stories published in the last 5 minutes.
- Recent Stories: Stories published in the last 5 minutes.
-
Real-time updates:
- Broadcasts new stories every 5 minutes.
Example Message:
{ "type": "recent-stories", "data": { "count": 2, "stories": [ { "title": "Story Title", "url": "https://story-url.com", "time_published": "2025-02-01T12:55:56.000Z" }, { "title": "Story Title", "url": "https://story-url.com", "time_published": "2025-02-01T12:55:56.000Z" } ] }