This repository contains two separate servers implemented in Node.js: a WebSocket server for real-time messaging and a RESTful API server for handling CRUD operations.
- Purpose: Manages real-time communication between clients.
- Technology: Built with the
ws
library in Node.js. - Functionality: Handles multiple client connections, broadcasts messages to all connected clients, and logs connection events and messages.
- Purpose: Provides a set of HTTP endpoints for CRUD operations.
- Technology: Built on Express.js.
- Endpoints: Includes endpoints for creating, reading, updating, and deleting user data.
- Database: Uses PostgreSQL for data storage.
- Node.js
- npm (Node.js package manager)
- PostgreSQL database
Clone the repository to your local machine:
git clone https://github.com/demoronator/Nodejs-demo.git
Navigate into each server directory (api-server and websocket-server) and install the required dependencies:
cd api-server
npm install
cd ../websocket-server
npm install
Create a .env file in each server directory with the necessary environment variables:
For api-server:
DB_USER=yourPostgresUsername
DB_HOST=yourDatabaseHost
DB_DATABASE=yourDatabaseName
DB_PASSWORD=yourDatabasePassword
DB_PORT=yourDatabasePort
For websocket-server: No additional configuration required.
To start each server, run the following command in their respective directories:
For the API server:
node server.js
For the WebSocket server:
node server.js
The WebSocket server listens on port 8080 and supports multiple client connections. It broadcasts any received message to all connected clients.
- Handling multiple client connections
- Broadcasting messages to all clients
- Connection and message logging
The RESTful API server provides endpoints for user management, including creating, retrieving, updating, and deleting users.
- POST
/users
: Create a new user - GET
/users
: Retrieve all users - GET
/users/:id
: Retrieve a single user by ID - PUT
/users/:id
: Update a user by ID - DELETE
/users/:id
: Delete a user by ID
- CRUD operations for user management
- Input validation and error handling
- Secure password storage with bcrypt
- Request logging
Both servers utilize winston for logging, which helps in monitoring server operations and debugging.