A Node.js Express server that provides REST API access to the NoCopyrightSounds music library using the nocopyrightsounds-api package.
- Node.js 16+ installed
- npm or yarn package manager
- Create project directory:
mkdir ncs-api-server
cd ncs-api-server- Initialize and install dependencies:
npm init -y
npm install nocopyrightsounds-api express cors
npm install -D nodemon-
Copy the server.js and package.json files to your project
-
Start the server:
# Development mode (with auto-restart)
npm run dev
# Production mode
npm startServer will run on http://localhost:3001
GET /api/songs?page=0&limit=20Response:
{
"success": true,
"data": [
{
"id": "song-id",
"name": "Song Title",
"artist": "Artist Name",
"genre": "House",
"mood": "Happy",
"previewUrl": "https://...",
"coverUrl": "https://...",
"download": {
"regular": "https://...",
"instrumental": "https://..."
}
}
],
"pagination": {
"page": 0,
"limit": 20,
"total": 20
}
}GET /api/search?q=elektronomia&genre=House&mood=Happy&page=0Parameters:
q(required): Search querygenre(optional): Genre filtermood(optional): Mood filterpage(optional): Page number (default: 0)
GET /api/artist/artist/760/srikarNote: The full artist path should be included after /api/artist/
GET /api/genresGET /api/moodsGET /api/download/:songId?type=regularParameters:
type:regularorinstrumental
GET /health// Proxy to Node.js server
Route::get('/ncs-proxy/{path}', function($path) {
$nodeUrl = 'http://localhost:3001/api/' . $path;
$queryString = request()->getQueryString();
if ($queryString) {
$nodeUrl .= '?' . $queryString;
}
$response = Http::get($nodeUrl);
return response()->json($response->json());
})->where('path', '.*');// Fetch songs
const fetchSongs = async (page = 0) => {
const response = await fetch(`/ncs-proxy/songs?page=${page}`);
const data = await response.json();
return data;
};
// Search songs
const searchSongs = async (query, filters = {}) => {
const params = new URLSearchParams({
q: query,
...filters
});
const response = await fetch(`/ncs-proxy/search?${params}`);
const data = await response.json();
return data;
};Import these example requests:
-
Get Songs:
- GET
http://localhost:3001/api/songs
- GET
-
Search:
- GET
http://localhost:3001/api/search?q=elektronomia
- GET
-
Get Genres:
- GET
http://localhost:3001/api/genres
- GET
-
Health Check:
- GET
http://localhost:3001/health
- GET
- Environment Variables:
# Create .env file
PORT=3001
NODE_ENV=production
CORS_ORIGIN=https://your-laravel-app.com- Update CORS settings:
app.use(cors({
origin: process.env.CORS_ORIGIN || 'http://localhost:8000'
}));- Add rate limiting:
npm install express-rate-limit- Add request logging:
npm install morgan-
"Module not found" errors:
- Make sure
"type": "module"is in package.json - Use ES6 import/export syntax
- Make sure
-
CORS errors:
- Check CORS configuration
- Ensure frontend URL is allowed
-
Port already in use:
- Change PORT in .env or kill existing process
- Use
lsof -ti:3001 | xargs kill -9
DEBUG=* npm run devMIT License - feel free to use this in your projects!
Happy coding! 🎵✨