Code Monkey home page Code Monkey logo

nodejs-demo's Introduction

Node.js WebSocket and RESTful API Servers

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.

Overview

WebSocket Server

  • 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.

RESTful API Server

  • 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.

Getting Started

Prerequisites

  • Node.js
  • npm (Node.js package manager)
  • PostgreSQL database

Installation

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

Configuration

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.

Running the Servers

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

WebSocket Server

The WebSocket server listens on port 8080 and supports multiple client connections. It broadcasts any received message to all connected clients.

Features

  • Handling multiple client connections
  • Broadcasting messages to all clients
  • Connection and message logging

RESTful API Server

The RESTful API server provides endpoints for user management, including creating, retrieving, updating, and deleting users.

Endpoints

  • 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

Features

  • CRUD operations for user management
  • Input validation and error handling
  • Secure password storage with bcrypt
  • Request logging

Logging

Both servers utilize winston for logging, which helps in monitoring server operations and debugging.

nodejs-demo's People

Contributors

demoronator avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.