Created
February 24, 2014 23:15
-
-
Save zorbash/9199285 to your computer and use it in GitHub Desktop.
Example api with nodejs and express framework
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
| express = require('express'); | |
| sqlite3 = require('sqlite3').verbose(); | |
| http = require('http'); | |
| exec = require('child_process').exec; | |
| db = new sqlite3.Database('/var/www/apps/shared/songs.db'); | |
| app = express(); | |
| app.listen(9999); | |
| app.get('/songs/random', function(req, res) { | |
| db.get('SELECT * FROM songs ORDER BY RANDOM() LIMIT 1;', function(err, row) { | |
| res.send(row); | |
| }); | |
| }); | |
| app.get('/words/random', function(req, res) { | |
| exec('shuf /usr/share/dict/words | head -1', function(err, stdout, stderr) { | |
| res.send({ word: stdout.trim() }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment