Skip to content

Instantly share code, notes, and snippets.

@judsonmitchell
Created April 16, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judsonmitchell/2cc46a86f8b9dbe48c9e5613d51e4c7c to your computer and use it in GitHub Desktop.
Save judsonmitchell/2cc46a86f8b9dbe48c9e5613d51e4c7c to your computer and use it in GitHub Desktop.
RESTful API using express and sqlite3
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('wedding.db');
const express = require('express');
const restapi = express();
restapi.get('/', function(req, res){
db.all('SELECT * FROM wedding', function(err, rows){
res.json(rows);
});
})
restapi.get('/gifts', function(req, res){
db.all("SELECT * FROM wedding where gift != ''", function(err, rows){
res.json(rows);
});
})
restapi.get('/gifts/count', function(req, res){
db.all("SELECT count(name) FROM wedding where gift != ''", function(err, rows){
res.json(rows);
});
})
restapi.get('/gifts/search/:searchterm', function(req, res){
var query = req.params.searchterm;
db.all("SELECT * FROM wedding where gift LIKE '%" + query + "%' or name LIKE '%" + query + "%'", function(err, rows){
res.json(rows);
});
})
restapi.listen(3000);
console.log("Submit GET or POST to http://localhost:3000/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment