Created
March 16, 2021 13:17
-
-
Save k1000/1bede1a4753ea1453ccbda11611d1d28 to your computer and use it in GitHub Desktop.
marvelouz
This file contains 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 crypto = require("crypto"); | |
const fetch = require('node-fetch'); | |
const mongoose = require('mongoose'); | |
const { auth } = require('express-openid-connect'); | |
require('dotenv').config(); | |
const PORT = process.env.PORT || 5000 | |
const mongo_uri = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@cluster0.u4tzh.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`; | |
mongoose.connect(mongo_uri, {useNewUrlParser: true, useUnifiedTopology: true}); | |
const Character = mongoose.model('Character', { name: String, id: Number, thumbnail: String }); | |
const MARVEL_API = "https://gateway.marvel.com:443/v1/public"; | |
const ts = new Date().getTime(); | |
const hash = crypto.createHash('md5').update( | |
`${ts}${process.env.MARVEL_PRIV_KEY}${process.env.MARVEL_PUB_KEY}`).digest('hex'); | |
const api_url = `${MARVEL_API}/characters?ts=${ts}&apikey=${process.env.MARVEL_PUB_KEY}&hash=${hash}`; | |
fetch(api_url) | |
.then(res => res.json()) | |
.then(json => json.data.results) | |
.then(data => { | |
//console.log(data) | |
data.map(ch => { | |
const character = new Character( | |
{name: ch.name, id:ch.id, thumbnail:ch.thumbnail.path}) | |
character.save().then(() => console.log(ch.name)); | |
}) | |
}) | |
const oauth_config = { | |
authRequired: true, | |
auth0Logout: true, | |
secret: process.env.OAUTH_SECRET, | |
baseURL: process.env.BASE_URL, | |
clientID: 'jLo23lqzF5fG7ZdCuPs9QvFJeA2napXr', | |
issuerBaseURL: process.env.OAUTH_ISSUER_BASE_URL | |
}; | |
express() | |
.use(auth(oauth_config)) | |
.get('/', (req, res) => Character.find({}) | |
.then(ch => res.send(req.oidc.isAuthenticated() ? JSON.stringify(ch) : 'Logged out')) | |
.catch(e => console.log(e)) | |
) | |
.get('/:id', (req, res) => Character.findOne({ id: parseInt(req.params.id)}) | |
.exec() | |
.then(ch => res.send(req.oidc.isAuthenticated() ? JSON.stringify(ch) : 'Logged out')) | |
.catch(e => console.log(e)) | |
) | |
.listen(PORT, () => console.log(`Listening on ${ PORT }`)) |
This file contains 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": "marvelous", | |
"version": "0.0.1", | |
"description": "marvelous app", | |
"engines": { | |
"node": "14.x" | |
}, | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "node test.js" | |
}, | |
"dependencies": { | |
"dotenv": "^8.2.0", | |
"express": "^4.17.1", | |
"express-openid-connect": "^2.3.0", | |
"mongoose": "^5.11.15", | |
"node-fetch": "^2.6.1" | |
}, | |
"devDependencies": { | |
"got": "^11.3.0", | |
"tape": "^4.7.0" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://github.com/heroku/node-js-getting-started" | |
}, | |
"license": "MIT" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment