Guest User

Untitled

a guest
Jan 7th, 2022
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // file deepcode ignore IncompleteRegex: I know what I do (.replace)
  2. const express = require('express')
  3. const fs = require('fs')
  4. const helmet = require('helmet')
  5. const mongoose = require('mongoose')
  6. const klaw = require('klaw')
  7.  
  8.  
  9. class API {
  10.     constructor(){
  11.         this.api = express()
  12.        
  13.         this.api.use(helmet())
  14.         this.api.disable('x-powered-by')
  15.         this.api.use(express.json())
  16.         this._loadApi()
  17.        
  18.         this.config = require('./config.json')
  19.     }
  20.  
  21.     async _loadApi() {
  22.         const apiDirectories = await fs.promises.readdir("./routes/")
  23.         let totalDirectories = 0;
  24.         for (const directory of apiDirectories) {
  25.             totalDirectories++;
  26.             this._loadHttpMethode(directory)
  27.         }
  28.         console.log(`[API] - Load ${totalDirectories} methods`)
  29.  
  30.         mongoose.connect("mongodb://localhost:27017/nobuyoshi_news", { useNewUrlParser: true })
  31.         const connection = mongoose.connection;
  32.  
  33.         connection.once('open', () => {
  34.             console.log(`[mongoose] - Successfully connected to the "${mongoose.connection.name}" database !`)
  35.  
  36.             this.api.get("/", async (req, res) => res.send("Bienvenue sur le root du site !"))
  37.  
  38.             this.api.listen(80, () => {
  39.                 console.log(`[API] - API started on port 80` )
  40.             })
  41.         })
  42.     }
  43.  
  44.     async _loadHttpMethode(method) {
  45.         switch (method) {
  46.             case 'post': {
  47.                 const posts = []
  48.                 klaw('./routes/post/')
  49.                 .on('data', post => {
  50.                     if(post.path.indexOf('post\\') && post.path.endsWith('.js')) {
  51.                         posts.push(post.path.substring(post.path.indexOf('post\\') + 5, post.path.lastIndexOf('.js')))
  52.                     } else if (post.path.endsWith('.js')) {
  53.                         posts.push(post.path)
  54.                     }
  55.                 })
  56.                 .on('end', () => {
  57.                     posts.forEach((post) => {
  58.                         const module = new (require(`./routes/post/${post}`))(this);
  59.                         this.api.post(`/${post}`, async (req, res) => await module.run(req, res))
  60.                     })
  61.                 })
  62.                 break;
  63.             }
  64.  
  65.             case 'get': {
  66.                 const gets = []
  67.                 klaw('./routes/get/')
  68.                 .on('data', get => {
  69.                     if(get.path.indexOf('get\\') && get.path.endsWith('.js')) {
  70.                         gets.push(get.path.substring(get.path.indexOf('get\\') + 4, get.path.lastIndexOf('.js')).replace('\\', '/'))
  71.                     } else if (get.path.endsWith('.js')) {
  72.                         gets.push(get.path)
  73.                     }
  74.                 })
  75.                 .on('end', () => {
  76.                     gets.forEach((get) => {
  77.                         const module = new (require(`./routes/get/${get}`))(this);
  78.                         this.api.get(`/${get}`, async (req, res) => await module.run(req, res))
  79.                     })
  80.                 })
  81.                 break;
  82.             }
  83.  
  84.             case 'put': {
  85.                 const puts = []
  86.                 klaw('./routes/put/')
  87.                 .on('data', put => {
  88.                     if(put.path.indexOf('put\\') && put.path.endsWith('.js')) {
  89.                         puts.push(put.path.substring(put.path.indexOf('put\\') + 4, put.path.lastIndexOf('.js')).replace('\\', '/'))
  90.                     } else if (put.path.endsWith('.js')) {
  91.                         puts.push(ut.path)
  92.                     }
  93.                 })
  94.                 .on('end', () => {
  95.                     puts.forEach((put) => {
  96.                         const module = new (require(`./routes/put/${put}`))(this);
  97.                         this.api.put(`/${put}`, async (req, res) => await module.run(req, res))
  98.                     })
  99.                 })
  100.                 break;
  101.             }
  102.        
  103.             default:
  104.                 break;
  105.         }
  106.     }
  107. }
  108.  
  109. module.exports = new API()
Advertisement
Add Comment
Please, Sign In to add comment