Skip to content

Instantly share code, notes, and snippets.

@shrunyan
Forked from anonymous/index.js
Created May 14, 2016 20:11
Show Gist options
  • Save shrunyan/aec0bc95a0050bf8106989f58c0aae8e to your computer and use it in GitHub Desktop.
Save shrunyan/aec0bc95a0050bf8106989f58c0aae8e to your computer and use it in GitHub Desktop.
Experimenting with Koajs and the Spotify api
'use strict'
const koa = require('koa')
const r = require('koa-route')
const https = require('https')
const app = koa()
app.use(r.get('/search/:term', function *(term) {
const req = https.request({
method: 'get',
protocol: 'https:',
host: 'api.spotify.com',
path: `/v1/search/q=${term}`
}, function *(res) {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.end()
})
req.on('error', function *(e) {
console.error('error', e)
})
req.end()
}))
app.use(function *() {
this.body = 'Hello World'
})
app.listen(3000)
{
"name": "spotify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"koa": "1.2.0",
"koa-route": "2.4.2"
},
"devDependencies": {
"babel": "6.5.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment