root commit
commit
ed0d0275de
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
packs/**
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"name": "beatstar_api",
|
||||
"version": "1.0.0",
|
||||
"description": "none",
|
||||
"main": "src/server.ts",
|
||||
"author": "ogomez",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"dev": "nodemon src/server.ts",
|
||||
"build": "rm -rf build/ && prettier --write src && tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.26.0",
|
||||
"express": "^4.17.3",
|
||||
"morgan": "^1.10.0",
|
||||
"nodemon": "^2.0.15",
|
||||
"ts-node": "^10.5.0",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/axios": "^0.14.0",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/morgan": "^1.9.3",
|
||||
"@types/node": "^17.0.18"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
import express from 'express';
|
||||
import PackService from '../services/pack_service';
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/packs', PackService.getPacks);
|
||||
|
||||
export = router;
|
|
@ -0,0 +1,43 @@
|
|||
import http from 'http';
|
||||
import express, { Express } from 'express';
|
||||
import morgan from 'morgan';
|
||||
import routes from './routes/packs';
|
||||
|
||||
const router: Express = express();
|
||||
|
||||
/** Logging */
|
||||
router.use(morgan('dev'));
|
||||
/** Parse the request */
|
||||
router.use(express.urlencoded({ extended: false }));
|
||||
/** Takes care of JSON data */
|
||||
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();
|
||||
});
|
||||
|
||||
/** Routes */
|
||||
router.use('/', routes);
|
||||
|
||||
/** Error handling */
|
||||
router.use((req, res, next) => {
|
||||
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;
|
||||
httpServer.listen(PORT, () => console.log(`The server is running on port ${PORT}`));
|
|
@ -0,0 +1,20 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import axios, { AxiosResponse } from "axios";
|
||||
|
||||
interface Pack {
|
||||
name: string;
|
||||
size: number;
|
||||
}
|
||||
|
||||
export default class PackService {
|
||||
public static getPacks = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
// get a list of all packs...
|
||||
return res.status(200).json({
|
||||
message: "hello all packs",
|
||||
});
|
||||
};
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src",
|
||||
"target": "es2021",
|
||||
"skipLibCheck": true,
|
||||
"strict": true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue