Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created February 24, 2014 23:15
Show Gist options
  • Select an option

  • Save zorbash/9199285 to your computer and use it in GitHub Desktop.

Select an option

Save zorbash/9199285 to your computer and use it in GitHub Desktop.
Example api with nodejs and express framework
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