Last active
July 5, 2018 09:47
-
-
Save joshterrill/1144ed8ed215fb038e94978e1020eff3 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
{ | |
"name": "usda-ingredients-api", | |
"version": "1.0.0", | |
"description": "a REST API for pulling ingredients given a UPC code from ndb.nal.usda.gov", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+https://github.com/joshterrill/usda-ingredients-api.git" | |
}, | |
"keywords": [ | |
"USDA", | |
"ingredient", | |
"UPC", | |
"ingredients", | |
"API" | |
], | |
"author": "Josh Terrill", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://github.com/joshterrill/usda-ingredients-api/issues" | |
}, | |
"homepage": "https://github.com/joshterrill/usda-ingredients-api#readme" | |
} |
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 app = express(); | |
const port = 3000; | |
app.get('/', (req, res) => { | |
res.send('Hello, world.'); | |
}); | |
app.listen(port, () => { | |
console.log(`Server 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
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.get('/', (req, res) => { | |
res.json({hello: 'world'}); | |
}); | |
app.post('/search', (req, res) => { | |
const body = req.body; | |
res.json({body}); | |
}); | |
app.listen(port, () => { | |
console.log(`Server 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
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const request = require('request'); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.get('/', (req, res) => { | |
res.json({hello: 'world'}); | |
}); | |
app.post('/search', (req, res) => { | |
const body = req.body; | |
if (body.text) { | |
request.get(`https://ndb.nal.usda.gov/ndb/search/list?qlookup=${body.text}`, (searchError, searchResponse, searchBody) => { | |
if (searchError) res.json({error: searchError, success: false, data: null}); | |
res.json({searchBody}); | |
}); | |
} else if (body.upc) { | |
// todo UPC code | |
} else { | |
res.json({error: 'Please provide a "text" or "upc" key in your request object.', success: false, data: null}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Server 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
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const request = require('request'); | |
const cheerio = require('cheerio'); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.get('/', (req, res) => { | |
res.json({hello: 'world'}); | |
}); | |
app.post('/search', (req, res) => { | |
const body = req.body; | |
if (body.text) { | |
request.get(`https://ndb.nal.usda.gov/ndb/search/list?qlookup=${body.text}`, (searchError, searchResponse, searchBody) => { | |
if (searchError) res.json({error: searchError, success: false, data: null}); | |
let $ = cheerio.load(searchBody.replace(/[\n\r\t]+/g, '')); | |
res.json({text: $('.result-message').text()}); | |
}); | |
} else if (body.upc) { | |
// todo UPC code | |
} else { | |
res.json({error: 'Please provide a "text" or "upc" key in your request object.', success: false, data: null}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Server 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
let $ = cheerio.load(searchBody.replace(/[\n\r\t]+/g, '')); | |
let searchResults = $($('.list-left table tbody tr')[0]).html(); | |
res.json({text: searchResults}); |
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 bodyParser = require('body-parser'); | |
const request = require('request'); | |
let cheerio = require('cheerio'); | |
const cheerioAdv = require('cheerio-advanced-selectors'); | |
cheerio = cheerioAdv.wrap(cheerio); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.get('/', (req, res) => { | |
res.json({hello: 'world'}); | |
}); | |
app.post('/search', (req, res) => { | |
const body = req.body; | |
if (body.text) { | |
request.get(`https://ndb.nal.usda.gov/ndb/search/list?qlookup=${body.text}`, (searchError, searchResponse, searchBody) => { | |
if (searchError) res.json({error: searchError, success: false, data: null}); | |
let $ = cheerio.load(searchBody.replace(/[\n\r\t]+/g, '')); | |
let searchProductLink = $($($($('.list-left table tbody tr')[0]).html()).children()[1]).attr('href'); | |
res.json({link: searchProductLink}); | |
}); | |
} else if (body.upc) { | |
// todo UPC code | |
} else { | |
res.json({error: 'Please provide a "text" or "upc" key in your request object.', success: false, data: null}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Server 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
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const request = require('request'); | |
let cheerio = require('cheerio'); | |
const cheerioAdv = require('cheerio-advanced-selectors'); | |
cheerio = cheerioAdv.wrap(cheerio); | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.get('/', (req, res) => { | |
res.json({hello: 'world'}); | |
}); | |
app.post('/search', (req, res) => { | |
const body = req.body; | |
if (body.text) { | |
request.get(`https://ndb.nal.usda.gov/ndb/search/list?qlookup=${body.text}`, (searchError, searchResponse, searchBody) => { | |
if (searchError) res.json({error: searchError, success: false, data: null}); | |
let $ = cheerio.load(searchBody.replace(/[\n\r\t]+/g, '')); | |
const searchProductLink = $($($($('.list-left table tbody tr')[0]).html()).children()[1]).attr('href'); | |
let detailLink = 'https://ndb.nal.usda.gov' + searchProductLink; | |
request.get(detailLink, (detailError, detailResponse, detailBody) => { | |
if (detailError) res.json({error: detailError, success: false, data: null}); | |
res.json({detailBody}); | |
}); | |
}); | |
} else if (body.upc) { | |
// todo UPC code | |
} else { | |
res.json({error: 'Please provide a "text" or "upc" key in your request object.', success: false, data: null}); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Server listening on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment