initial commit

parents
module.exports = {
parser: '@typescript-eslint/parser',
extends: ['plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
env: {
es6: true,
node: true,
},
rules: {
'no-var': 'error',
semi: 'error',
indent: ['error', 2, { SwitchCase: 1 }],
'no-multi-spaces': 'error',
'space-in-parens': 'error',
'no-multiple-empty-lines': 'error',
'prefer-const': 'error',
},
};
.env
.env.*
node_modules
dist
build
.idea
.vscode
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 150,
"tabWidth": 2
}
{
"watch": [ "src" ],
"ext": "ts",
"exec": "ts-node ./src/index.ts"
}
{
"name": "node_template",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon",
"lint": "eslint",
"lint:fix": "eslint --fix"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"ts-node": "^10.9.1",
"tslib": "^2.6.0",
"typescript": "^5.1.6"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^20.4.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.0"
}
}
import express from 'express';
import { Application, RequestHandler } from 'express';
import { AppInit } from './interfaces/AppInit.interface';
import { IRoute } from './interfaces/IRoute.interface';
class App {
public app: Application;
public port: number;
constructor(appInit: AppInit) {
this.app = express();
this.port = appInit.port;
this.initMiddlewares(appInit.middlewares);
this.initRoutes(appInit.controllers);
this.initAssets();
}
private initMiddlewares(middlewares: RequestHandler[]) {
middlewares.forEach((middleware) => {
this.app.use(middleware);
});
}
private initRoutes(routes: IRoute[]) {
routes.forEach((route) => {
this.app.use(route.path, route.router);
});
}
private initAssets() {
this.app.use(express.json());
}
public listen() {
this.app.listen(this.port, () => {
console.log(`App listening on the http://localhost:${this.port}`);
});
}
}
export default App;
import { RequestHandler } from 'express';
import { ArticleService } from '../services/article.service';
export class ArticleController {
private service: ArticleService;
constructor() {
this.service = new ArticleService();
}
getAllArticles: RequestHandler = (req, res): void => {
const articles = this.service.getAllArticles;
res.send(articles);
};
getArticle: RequestHandler = (req, res): void => {
const article = this.service.getArticle(req.params.id);
res.send(article);
};
createArticle: RequestHandler = (req, res): void => {
const article = this.service.createArticle(req.body);
res.send(article);
};
}
import App from './app';
import logger from './middlewares/logger';
import { ArticleRoute } from './routes/article.route';
const app = new App({
port: 8000,
middlewares: [logger()],
controllers: [new ArticleRoute()],
});
app.listen();
import { RequestHandler } from 'express';
import { IRoute } from './IRoute.interface';
export interface AppInit {
port: number;
middlewares: RequestHandler[];
controllers: IRoute[];
}
export interface IArticle {
id: string;
title: string;
description: string;
}
import { Router } from 'express';
export interface IRoute {
path: string;
router: Router;
}
import { RequestHandler } from 'express';
const logger = (): RequestHandler => (req, res, next) => {
console.log(`Request logged: ${req.method}, ${req.path}`);
next();
};
export default logger;
import { Router } from 'express';
import { ArticleController } from '../controllers/article.controller';
import { IRoute } from '../interfaces/IRoute.interface';
export class ArticleRoute implements IRoute {
public path = '/articles';
public router = Router();
private controller: ArticleController;
constructor() {
this.controller = new ArticleController();
this.init();
}
private init() {
this.router.get('/', this.controller.getAllArticles);
this.router.get('/:id', this.controller.getArticle);
this.router.post('/', this.controller.createArticle);
}
}
import { IArticle } from '../interfaces/IArticle.interface';
export class ArticleService {
private articles: IArticle[] = [];
getAllArticles = (): IArticle[] => {
return this.articles;
};
getArticle = (id: string): IArticle => {
const article = this.articles.find((article) => article.id === id);
if (article) return article;
else throw new Error('invalid id');
};
createArticle = (data: IArticle): IArticle => {
return {
id: Math.random().toString(),
title: data.title,
description: data.description,
};
};
}
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"outDir": "dist",
"importHelpers": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"esModuleInterop": true,
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment