Skip to content

Licoler/BackendApp_Baratov

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backend Express Application

This repository contains a simple backend application built with Express and TypeScript. The API provides user authentication and CRUD operations for posts. It uses PostgreSQL as the database and Prisma as the ORM.

Features

  • User registration and login with JWT-based authentication
  • Protected routes for creating, updating and deleting posts
  • Public endpoints to view posts
  • Prisma schema for User and Post models

Requirements

  • Node.js (v16 or later recommended)
  • PostgreSQL database
  • npm or yarn

Setup

  1. Clone the repository

    git clone <repo-url>
    cd backend-2
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Database configuration

    Edit the .env file at the project root and set the DATABASE_URL value to point to your PostgreSQL instance. Example:

    DATABASE_URL="postgresql://postgres:postgres@localhost:5432/shop"
  4. Generate Prisma client and run migrations

    npx prisma generate
    npx prisma migrate dev --name init
  5. Set JWT secret (optional)

    By default the code uses a hardcoded secret ("secret"). To use a custom value, set the JWT_SECRET environment variable:

    export JWT_SECRET="your_secret_here"
  6. Start the development server

    npm run dev

    The server will listen on http://localhost:3000.

API Endpoints

Authentication

  • POST /api/auth/register

    • Request body: { username, email, password }
    • Registers a new user.
  • POST /api/auth/login

    • Request body: { email, password }
    • Returns a JWT token and user details.
  • POST /api/auth/logout

    • No body required. Endpoint responds with confirmation; token invalidation is handled by the client.

Posts

  • GET /api/posts – returns a list of all posts with author info.
  • GET /api/posts/:id – returns a single post.
  • POST /api/posts – create a new post (requires Authorization: Bearer <token>).
  • PUT /api/posts/:id – update a post (token of the author required).
  • DELETE /api/posts/:id – delete a post (token of the author required).

Notes

  • Passwords are hashed with bcrypt before storage.
  • JWTs expire after 1 hour.
  • The auth middleware checks the Authorization header and attaches the user payload to the request.

Feel free to extend the project with additional models, routes, or middleware as needed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors