NodeJS
What is NodeJS?
NodeJS is a runtime that allows you to write complex applications using JavaScript.
NodeJS comes with a package manager npm, which contains thousands of packages that do exactly what you need in your app. For ours, we're going to use it to access Spotify.
Client/Server architecture
We'll be using Node.JS for our backend, but before we go into that, we need to make sure you understand how web works.
We have a client and a server in web. Generally the client will make requests to the server. The server will figure out whether the client is authorized to make such a request. If the client's request is accepted, then the server will respond with the correct data. Otherwise, the server sends the proper error code.
A client is generally something like a browser, mobile device, etc.
A server is any machine that is constantly listening for requests.
Generally, when you open up google.com, your browser issues a request to the server asking for the correct html page. Then, your server responds with that html page. The browser realizes that you have scripts and css files needed, so it issues those requests. If your server is set up properly, then the server responds with the correct scripts and css files.
Endpoints
Generally, with a server, you have different endpoints that do different things. We will have a few
/search/artist/:artistName //search for a particular artist
/search/song/:songName //search for a particular song
In the server-side, we'll handle the authenticating and getting the data from Spotify.
Setting up your app
npm init
npm install --save spotify-web-api express
Next, create a file called index.js. Let's go ahead and import stuff
var express = require('express');
var SpotifyWebApi = require('spotify-web-api');
var spotifyApi = new SpotifyWebApi({
clientId: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
redirectUri: 'localhost:3000/callback'
});
var app = express(),
port = process.env.PORT || 3000;
Replace CLIENT_ID and CLIENT_SECRET with your authentication information from Spotify.
Next, we want to serve our files directly from the www folder, so we need to add the line
app.use(express.static(__dirname + '/www'));
Setting up endpoints
We want to make it so that when we make HTTP GET requests from the client, the server will request Spotify for the correct information then send back once available.
app.get('/search/artist/:artistName', function(req, res) {
//handle request
});
The above sets up an endpoint so when we ask for /search/artist/:artistName we get the proper artist name.
artistName is the parameter name of the artist. To access it, we can just do req.params.artistName
So, let's go ahead and search for the artist!
Searching for the artist
We need to make a request for the artist from the spotifyApi. The method for this is searchArtists
All we need to do is get the name of the artist and send it to the searchArtists method.
app.get('search/artist/:artistName', function(req, res) {
spotifyApi.searchArtists(req.params.artistName, { limit: 10 }, function(err, data) {
if(err) {
console.err(err);
} else {
console.log(data.body);
}
});
Sending the response
Ok, now we have the information logged, but we want to send that information back to the client, right? So let's just replace the console.log with res.send
res.send will send the data to the client as a Response
...
if(err) {
console.err(err);
} else {
res.send(data.body);
}
...
Running the server
We can run the server by typing node index.js