Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file deepcode ignore IncompleteRegex: I know what I do (.replace)
- const express = require('express')
- const fs = require('fs')
- const helmet = require('helmet')
- const mongoose = require('mongoose')
- const klaw = require('klaw')
- class API {
- constructor(){
- this.api = express()
- this.api.use(helmet())
- this.api.disable('x-powered-by')
- this.api.use(express.json())
- this._loadApi()
- this.config = require('./config.json')
- }
- async _loadApi() {
- const apiDirectories = await fs.promises.readdir("./routes/")
- let totalDirectories = 0;
- for (const directory of apiDirectories) {
- totalDirectories++;
- this._loadHttpMethode(directory)
- }
- console.log(`[API] - Load ${totalDirectories} methods`)
- mongoose.connect("mongodb://localhost:27017/nobuyoshi_news", { useNewUrlParser: true })
- const connection = mongoose.connection;
- connection.once('open', () => {
- console.log(`[mongoose] - Successfully connected to the "${mongoose.connection.name}" database !`)
- this.api.get("/", async (req, res) => res.send("Bienvenue sur le root du site !"))
- this.api.listen(80, () => {
- console.log(`[API] - API started on port 80` )
- })
- })
- }
- async _loadHttpMethode(method) {
- switch (method) {
- case 'post': {
- const posts = []
- klaw('./routes/post/')
- .on('data', post => {
- if(post.path.indexOf('post\\') && post.path.endsWith('.js')) {
- posts.push(post.path.substring(post.path.indexOf('post\\') + 5, post.path.lastIndexOf('.js')))
- } else if (post.path.endsWith('.js')) {
- posts.push(post.path)
- }
- })
- .on('end', () => {
- posts.forEach((post) => {
- const module = new (require(`./routes/post/${post}`))(this);
- this.api.post(`/${post}`, async (req, res) => await module.run(req, res))
- })
- })
- break;
- }
- case 'get': {
- const gets = []
- klaw('./routes/get/')
- .on('data', get => {
- if(get.path.indexOf('get\\') && get.path.endsWith('.js')) {
- gets.push(get.path.substring(get.path.indexOf('get\\') + 4, get.path.lastIndexOf('.js')).replace('\\', '/'))
- } else if (get.path.endsWith('.js')) {
- gets.push(get.path)
- }
- })
- .on('end', () => {
- gets.forEach((get) => {
- const module = new (require(`./routes/get/${get}`))(this);
- this.api.get(`/${get}`, async (req, res) => await module.run(req, res))
- })
- })
- break;
- }
- case 'put': {
- const puts = []
- klaw('./routes/put/')
- .on('data', put => {
- if(put.path.indexOf('put\\') && put.path.endsWith('.js')) {
- puts.push(put.path.substring(put.path.indexOf('put\\') + 4, put.path.lastIndexOf('.js')).replace('\\', '/'))
- } else if (put.path.endsWith('.js')) {
- puts.push(ut.path)
- }
- })
- .on('end', () => {
- puts.forEach((put) => {
- const module = new (require(`./routes/put/${put}`))(this);
- this.api.put(`/${put}`, async (req, res) => await module.run(req, res))
- })
- })
- break;
- }
- default:
- break;
- }
- }
- }
- module.exports = new API()
Advertisement
Add Comment
Please, Sign In to add comment