fix: Add base path constant

master
ogomez-at-wiris 2022-02-20 15:01:03 +01:00
parent 20c979fc12
commit 9ea34bdc30
3 changed files with 32 additions and 25 deletions

View File

@ -1,13 +1,13 @@
import http from 'http'; import http from "http";
import express, { Express } from 'express'; import express, { Express } from "express";
import morgan from 'morgan'; import morgan from "morgan";
import { packRoutes } from './routes/packs'; import { packRoutes } from "./routes/packs";
import PackService from './services/pack_service'; import PackService from "./services/pack_service";
const router: Express = express(); const router: Express = express();
/** Logging */ /** Logging */
router.use(morgan('dev')); router.use(morgan("dev"));
/** Parse the request */ /** Parse the request */
router.use(express.urlencoded({ extended: false })); router.use(express.urlencoded({ extended: false }));
/** Takes care of JSON data */ /** Takes care of JSON data */
@ -15,33 +15,37 @@ router.use(express.json());
/** RULES OF OUR API */ /** RULES OF OUR API */
router.use((req, res, next) => { router.use((req, res, next) => {
// set the CORS policy // set the CORS policy
res.header('Access-Control-Allow-Origin', '*'); res.header("Access-Control-Allow-Origin", "*");
// set the CORS headers // set the CORS headers
res.header('Access-Control-Allow-Headers', 'origin, X-Requested-With,Content-Type,Accept, Authorization'); res.header(
// set the CORS method headers "Access-Control-Allow-Headers",
if (req.method === 'OPTIONS') { "origin, X-Requested-With,Content-Type,Accept, Authorization"
res.header('Access-Control-Allow-Methods', 'GET POST'); );
return res.status(200).json({}); // set the CORS method headers
} if (req.method === "OPTIONS") {
next(); res.header("Access-Control-Allow-Methods", "GET POST");
return res.status(200).json({});
}
next();
}); });
/** Routes */ /** Routes */
router.use('/', packRoutes); router.use("/", packRoutes);
/** Error handling */ /** Error handling */
router.use((req, res, next) => { router.use((req, res, next) => {
const error = new Error('not found'); const error = new Error("not found");
return res.status(404).json({ return res.status(404).json({
message: error.message message: error.message,
}); });
}); });
/** Server */ /** Server */
const httpServer = http.createServer(router); const httpServer = http.createServer(router);
const PORT: any = process.env.PORT ?? 6060; const PORT: any = process.env.PORT ?? 6060;
PackService.getInitialList().then(() => { 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}`)
);
}); });

View File

@ -1,18 +1,20 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import fs from "fs"; import fs from "fs";
import axios, { AxiosResponse } from "axios";
import * as fsWalk from "@nodelib/fs.walk"; import * as fsWalk from "@nodelib/fs.walk";
const PACK_BASE_PATH = '';
interface Pack { interface Pack {
name: string; name: string;
size: number; size: number;
bpm: string;
} }
export default class PackService { export default class PackService {
public static getInitialList = async () => { public static getInitialList = async () => {
const packs = await fs.readdirSync("packs"); const packs = await fs.readdirSync(PACK_BASE_PATH+"packs");
console.log(packs); console.log(packs);
}; };
public static getPacks = async ( public static getPacks = async (
req: Request, req: Request,
res: Response, res: Response,

View File

@ -2,6 +2,7 @@
"compilerOptions": { "compilerOptions": {
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"esModuleInterop": true, "esModuleInterop": true,
"module": "commonjs",
"outDir": "./build", "outDir": "./build",
"rootDir": "./src", "rootDir": "./src",
"target": "es2021", "target": "es2021",