Skip to content

Naman501/Spotify_Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spotify Backend

A Node.js and Express backend for a Spotify-like application. This API supports:

  • User and artist authentication with JWT stored in cookies
  • Music upload (artist only)
  • Album creation (artist only)
  • Fetching music and albums (user only)

Tech Stack

  • Node.js
  • Express
  • MongoDB with Mongoose
  • JWT Authentication
  • Multer (file upload)
  • ImageKit (music file storage)

Project Structure

.
|-- server.js
|-- src
|   |-- app.js
|   |-- controllers
|   |   |-- auth.controller.js
|   |   |-- music.controller.js
|   |-- db
|   |   |-- db.js
|   |-- middleware
|   |   |-- auth.middleware.js
|   |-- models
|   |   |-- album.model.js
|   |   |-- music.model.js
|   |   |-- user.model.js
|   |-- routes
|   |   |-- auth.routes.js
|   |   |-- music.routes.js
|   |-- services
|       |-- storage.service.js

Installation

  1. Clone the repository:
git clone https://github.com/Naman501/Spotify_Backend.git
cd Spotify_Backend
  1. Install dependencies:
npm install
  1. Create a .env file in the project root.

  2. Start the server in development mode:

npm run dev

For production:

npm start

Environment Variables

Create a .env file with the following variables:

PORT=3000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
IMAGEKIT_PRIVATE_KEY=your_imagekit_private_key

Notes:

  • IMAGEKIT_PRIVATE_KEY is required by the storage service used for uploads.
  • Ensure your ImageKit account configuration allows file uploads using the provided key.

API Base URL

Local:

http://localhost:3000

Routes

Health Route

  • GET /
  • Response: Spotify Backend!

Auth Routes

Base path: /api/v1/auth

  1. POST /register
  • Register a new user or artist.
  • Body:
{
  "username": "john",
  "email": "john@example.com",
  "password": "password123",
  "role": "user"
}

role can be user or artist.

  1. POST /login
  • Login with username or email and password.
  • Body:
{
  "email": "john@example.com",
  "password": "password123"
}

or

{
  "username": "john",
  "password": "password123"
}
  1. POST /logout
  • Clears the auth cookie.

Music Routes

Base path: /api/v1/music

  1. POST /upload (Artist only)
  • Middleware: authArtist
  • Upload field name: music
  • Content type: multipart/form-data
  • Body fields:
    • title (string)
    • music (file)
  1. POST /album (Artist only)
  • Middleware: authArtist
  • Body:
{
  "title": "My Album",
  "musics": ["musicObjectId1", "musicObjectId2"]
}
  1. GET / (User only)
  • Middleware: authUser
  • Returns music list.
  1. GET /albums (User only)
  • Middleware: authUser
  • Returns albums with populated music references.

Authentication and Roles

The API uses a cookie named token.

  • authArtist allows only users with role artist.
  • authUser allows only users with role user.

Important behavior:

  • Artist endpoints cannot be called by user role accounts.
  • User endpoints cannot be called by artist role accounts.

Example Workflow

  1. Register an artist account.
  2. Login as artist.
  3. Upload music using POST /api/v1/music/upload.
  4. Create an album using POST /api/v1/music/album.
  5. Register/login as a user account.
  6. Fetch music and albums using user-only endpoints.

Available Scripts

  • npm run dev - Start server with nodemon
  • npm start - Start server with Node.js
  • npm test - Placeholder test script

Repository

About

A Node.js and Express backend for a Spotify-like application.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors