I'm learning node and I'm trying to use the Spotify API to search and return an artist. The page loads and everything, but then when I try to search, I get this error
undefined:1
<html>
^
SyntaxError: Unexpected token <
at Object.parse (native)
at IncomingMessage.<anonymous> (/Users/edwinzhang/Node_Courses/spotify-recommend/server.js:23:25)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
After some digging, I found the reason I was getting this error was due to this:
var searchReq = http.get(options, function(response) {
response.on('data', function(chunk) {
item += chunk;
console.log(item);
});
response.on('end', function() {
console.log('end');
console.log(item);
item = JSON.parse(item);
emitter.emit('end', item);
});
response.on('error', function() {
emitter.emit('error');
});
});
In response.on('data', function(chunk) ...
, chunk was returning
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
The path for the request is (supposedly) api.spotify.com/v1/search?q=sam&limit=1&type=artist. Does anyone know why I am getting this error? Thanks!