Created
November 10, 2018 03:48
-
-
Save iamdtang/66fc44fdafbbff894f76986e07b50bfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const knex = require('knex'); | |
const app = express(); | |
const port = process.env.PORT || 8000; | |
app.get('/api/genres', function(request, response) { | |
let knex = connect(); | |
knex.select().from('genres').then((genres) => { | |
response.json(genres); | |
}); | |
}); | |
app.get('/api/genres/:id', function(request, response) { | |
let knex = connect(); | |
let id = request.params.id; | |
knex | |
.select() | |
.from('genres') | |
.where('GenreId', id) | |
.first() | |
.then((genres) => { | |
response.json(genres); | |
}); | |
}); | |
function connect() { | |
return knex({ | |
client: 'sqlite3', | |
connection: { | |
filename: './database.sqlite' | |
} | |
}); | |
} | |
app.listen(port, function() { | |
console.log(`Listening on port ${port}`); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"dependencies": { | |
"express": "^4.16.2", | |
"knex": "^0.14.4", | |
"sqlite3": "^3.1.13" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment