diff --git a/src/server.ts b/src/server.ts index af1ab0b..2bbbf4b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,13 +1,13 @@ -import http from 'http'; -import express, { Express } from 'express'; -import morgan from 'morgan'; -import { packRoutes } from './routes/packs'; -import PackService from './services/pack_service'; +import http from "http"; +import express, { Express } from "express"; +import morgan from "morgan"; +import { packRoutes } from "./routes/packs"; +import PackService from "./services/pack_service"; const router: Express = express(); /** Logging */ -router.use(morgan('dev')); +router.use(morgan("dev")); /** Parse the request */ router.use(express.urlencoded({ extended: false })); /** Takes care of JSON data */ @@ -15,33 +15,37 @@ router.use(express.json()); /** RULES OF OUR API */ router.use((req, res, next) => { - // set the CORS policy - res.header('Access-Control-Allow-Origin', '*'); - // set the CORS headers - res.header('Access-Control-Allow-Headers', 'origin, X-Requested-With,Content-Type,Accept, Authorization'); - // set the CORS method headers - if (req.method === 'OPTIONS') { - res.header('Access-Control-Allow-Methods', 'GET POST'); - return res.status(200).json({}); - } - next(); + // set the CORS policy + res.header("Access-Control-Allow-Origin", "*"); + // set the CORS headers + res.header( + "Access-Control-Allow-Headers", + "origin, X-Requested-With,Content-Type,Accept, Authorization" + ); + // set the CORS method headers + if (req.method === "OPTIONS") { + res.header("Access-Control-Allow-Methods", "GET POST"); + return res.status(200).json({}); + } + next(); }); /** Routes */ -router.use('/', packRoutes); +router.use("/", packRoutes); /** Error handling */ router.use((req, res, next) => { - const error = new Error('not found'); - return res.status(404).json({ - message: error.message - }); + const error = new Error("not found"); + return res.status(404).json({ + message: error.message, + }); }); /** Server */ const httpServer = http.createServer(router); const PORT: any = process.env.PORT ?? 6060; PackService.getInitialList().then(() => { - httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`)); + httpServer.listen(PORT, () => + console.log(`The server is running on port ${PORT}`) + ); }); - diff --git a/src/services/pack_service.ts b/src/services/pack_service.ts index 2397d7b..45fc267 100644 --- a/src/services/pack_service.ts +++ b/src/services/pack_service.ts @@ -1,18 +1,20 @@ import { Request, Response, NextFunction } from "express"; import fs from "fs"; -import axios, { AxiosResponse } from "axios"; import * as fsWalk from "@nodelib/fs.walk"; +const PACK_BASE_PATH = ''; interface Pack { name: string; size: number; + bpm: string; } export default class PackService { public static getInitialList = async () => { - const packs = await fs.readdirSync("packs"); + const packs = await fs.readdirSync(PACK_BASE_PATH+"packs"); console.log(packs); }; + public static getPacks = async ( req: Request, res: Response, diff --git a/tsconfig.json b/tsconfig.json index f998c3a..e4a79f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "forceConsistentCasingInFileNames": true, "esModuleInterop": true, + "module": "commonjs", "outDir": "./build", "rootDir": "./src", "target": "es2021",