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)
- Node.js
- Express
- MongoDB with Mongoose
- JWT Authentication
- Multer (file upload)
- ImageKit (music file storage)
.
|-- 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
- Clone the repository:
git clone https://github.com/Naman501/Spotify_Backend.git
cd Spotify_Backend- Install dependencies:
npm install-
Create a
.envfile in the project root. -
Start the server in development mode:
npm run devFor production:
npm startCreate 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_keyNotes:
IMAGEKIT_PRIVATE_KEYis required by the storage service used for uploads.- Ensure your ImageKit account configuration allows file uploads using the provided key.
Local:
http://localhost:3000
GET /- Response:
Spotify Backend!
Base path: /api/v1/auth
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.
POST /login
- Login with username or email and password.
- Body:
{
"email": "john@example.com",
"password": "password123"
}or
{
"username": "john",
"password": "password123"
}POST /logout
- Clears the auth cookie.
Base path: /api/v1/music
POST /upload(Artist only)
- Middleware:
authArtist - Upload field name:
music - Content type:
multipart/form-data - Body fields:
title(string)music(file)
POST /album(Artist only)
- Middleware:
authArtist - Body:
{
"title": "My Album",
"musics": ["musicObjectId1", "musicObjectId2"]
}GET /(User only)
- Middleware:
authUser - Returns music list.
GET /albums(User only)
- Middleware:
authUser - Returns albums with populated music references.
The API uses a cookie named token.
authArtistallows only users with roleartist.authUserallows only users with roleuser.
Important behavior:
- Artist endpoints cannot be called by
userrole accounts. - User endpoints cannot be called by
artistrole accounts.
- Register an artist account.
- Login as artist.
- Upload music using
POST /api/v1/music/upload. - Create an album using
POST /api/v1/music/album. - Register/login as a user account.
- Fetch music and albums using user-only endpoints.
npm run dev- Start server with nodemonnpm start- Start server with Node.jsnpm test- Placeholder test script