aldikhan13

Kraken-Injection Demo

Jul 22nd, 2021 (edited)
1,000
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require('kraken-injection').config() // or like this -> require('kraken-injection/dist/config')
  2. const express = require('express')
  3. const http = require('http')
  4.  
  5. const app = express()
  6. const server = http.createServer(app)
  7.  
  8. app.get('/', async (req, res) => {
  9.     try {
  10.         const response = await $axios.get('https://jsonplaceholder.typicode.com/users')
  11.         res.status(200).json({ users: response.data })
  12.     } catch (error) {
  13.         res.status(400).json({ error })
  14.     }
  15. })
  16.  
  17. app.get('/:id', async (req, res) => {
  18.     try {
  19.         const response = await $axios.get('https://jsonplaceholder.typicode.com/users')
  20.         const data = $_.find(response.data, (val) => val.id === parseInt(req.params.id) && val)
  21.         res.status(200).json({ user: data })
  22.     } catch (error) {
  23.         res.status(400).json({ error })
  24.     }
  25. })
  26.  
  27. server.listen(process.env.NODE_ENV || 4000, () => console.log(`server running on port ${server.address().port}`))
  28.  
  29. /*
  30. * =======================
  31. * kraken.config.json
  32. *========================
  33. {
  34.     "packages": [
  35.         {
  36.             "name": "$axios",
  37.             "module": "axios"
  38.         },
  39.         {
  40.             "name": "$_",
  41.             "module": "lodash",
  42.             "inject": false // If inject set to false, module cannot be access to
  43.                             // global and default value for inject is true
  44.         }
  45.     ]
  46. }
  47.  */
RAW Paste Data