Skip to content

Instantly share code, notes, and snippets.

@emilyruby
Created November 16, 2017 17:26
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 emilyruby/156bc533feb0ee05f909a21ff828cf5d to your computer and use it in GitHub Desktop.
Save emilyruby/156bc533feb0ee05f909a21ff828cf5d to your computer and use it in GitHub Desktop.
const algoliasearch = require('algoliasearch')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const client = algoliasearch('H1LZZCXWZT', process.env.API_KEY)
const index = client.initIndex('movies')
app.get('/', (req, res) => res.send('GET'))
app.post('/api/1/movies', bodyParser.json(), async (req, res) => {
if (req.body) {
try {
const content = await index.addObjects([req.body])
res.send(content.objectIDs[0])
} catch (e) {
console.error(e)
res.status(500).send('ERROR')
}
}
})
app.delete('/api/1/movies/:id', async (req, res) => {
try {
const result = await index.deleteObject(req.params.id)
res.send(result.objectID)
} catch (e) {
console.error(e)
res.status(500).send('ERROR')
}
res.send('DELETE')
})
app.listen(9000, () => console.log('Example app is listening on port 9000!'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment