From bbc898114882433f5249c05113f6344d81a39ba2 Mon Sep 17 00:00:00 2001 From: Mario Arita Date: Tue, 27 Feb 2024 12:15:04 -0600 Subject: [PATCH] feat(init): :tada: Initialize Express.js template --- .dockerignore | 6 + .eslintrc.json | 41 +++++++ .gitignore | 139 ++---------------------- .vscode/settings.json | 16 +++ Dockerfile | 44 ++++++++ config/default.json | 0 config/development.json | 0 config/production.json | 0 package.json | 33 ++++++ src/api/controllers/testController.ts | 22 ++++ src/api/middlewares/globalMiddleware.ts | 7 ++ src/api/routes/testRoutes.ts | 11 ++ src/app.ts | 20 ++++ src/types.d.ts | 11 ++ src/utils/environment.js | 0 tsconfig.json | 23 ++++ 16 files changed, 246 insertions(+), 127 deletions(-) create mode 100644 .dockerignore create mode 100644 .eslintrc.json create mode 100644 .vscode/settings.json create mode 100644 Dockerfile create mode 100644 config/default.json create mode 100644 config/development.json create mode 100644 config/production.json create mode 100644 package.json create mode 100644 src/api/controllers/testController.ts create mode 100644 src/api/middlewares/globalMiddleware.ts create mode 100644 src/api/routes/testRoutes.ts create mode 100644 src/app.ts create mode 100644 src/types.d.ts create mode 100644 src/utils/environment.js create mode 100644 tsconfig.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9e3effd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +node_modules +.vscode +.gitignore +README.md +.dockerignore +.git \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..963a332 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,41 @@ +{ + "env": { + "es2021": true, + "node": true + }, + "extends": [ + "standard", + "plugin:@typescript-eslint/recommended" + ], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module", + "project": "./tsconfig.json" + }, + "plugins": [ + "@typescript-eslint" + ], + "rules": { + "indent": ["error",2, + { + "SwitchCase": 1, + "flatTernaryExpressions": true + } + ], + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/explicit-module-boundary-types": "off", + "@typescript-eslint/no-var-requires": "off", + "@typescript-eslint/prefer-nullish-coalescing": "off", + "@typescript-eslint/strict-boolean-expressions": "off", + "@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }] + }, + "overrides": [ + { + "files": ["*.ts"], + "rules": { + "no-undef": "off" + } + } + ] +} diff --git a/.gitignore b/.gitignore index ceaea36..d6c075b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,132 +1,17 @@ -# ---> Node +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +node_modules + +# build +dist + # Logs logs *.log npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* -.pnpm-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# Snowpack dependency directory (https://snowpack.dev/) -web_modules/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional stylelint cache -.stylelintcache - -# Microbundle cache -.rpt2_cache/ -.rts2_cache_cjs/ -.rts2_cache_es/ -.rts2_cache_umd/ - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variable files -.env -.env.development.local -.env.test.local -.env.production.local -.env.local - -# parcel-bundler cache (https://parceljs.org/) -.cache -.parcel-cache - -# Next.js build output -.next -out - -# Nuxt.js build / generate output -.nuxt -dist - -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and not Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# vuepress v2.x temp and cache directory -.temp -.cache - -# Docusaurus cache and generated files -.docusaurus - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# TernJS port file -.tern-port - -# Stores VSCode versions used for testing VSCode extensions -.vscode-test - -# yarn v2 -.yarn/cache -.yarn/unplugged -.yarn/build-state.yml -.yarn/install-state.gz -.pnp.* +# lockfile +package-lock.json +yarn.lock +pnpm-lock.yaml diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9a69805 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,16 @@ +{ + "editor.tabSize": 2, + "editor.detectIndentation": false, + "search.exclude": { + "package-lock.json": true + }, + "editor.defaultFormatter": "dbaeumer.vscode-eslint", + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.addMissingImports": "explicit", + "source.fixAll.eslint": "explicit" + }, + "cSpell.words": [ + "orbis" + ] +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..62161e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +## Build image and name it 'express-template' +# docker build -t express-template . + +## Run container and name it 'ExpressApi'. Webpage is localhost:9000 +# docker run -it --rm -dp 9000:9000 --name ExpressApi express-template + +## Connect to container +# docker exec -it ExpressApi bash + +## Show container log +# docker container logs ExpressApi + +## Stop docker container +# docker stop ExpressApi + +## All together +# docker stop ExpressApi & docker image rm -f express-template & docker build -t express-template . && docker run -it --rm -dp 9000:9000 --name ExpressApi express-template && docker exec -it ExpressApi sh + +# Start Dockerfile +ARG VERSION=18.16.0-bullseye-slim +ARG DIR=ExpressApi + +FROM node:${VERSION} as builder +ARG DIR +WORKDIR /${DIR} +COPY . . +RUN apt-get update && \ + apt-get install -y git && \ + yarn install --production && \ + yarn build:prod + +# Run Stage +FROM node:${VERSION} as runner +ARG DIR +WORKDIR /${DIR} +COPY --from=builder /${DIR}/dist ./dist +COPY --from=builder /${DIR}/node_modules ./node_modules + +ENV TZ="America/Guatemala" +ENV HOST=0.0.0.0 +ENV PORT=9000 + +EXPOSE 9000 +ENTRYPOINT ["node", "dist/app.js"] diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..e69de29 diff --git a/config/development.json b/config/development.json new file mode 100644 index 0000000..e69de29 diff --git a/config/production.json b/config/production.json new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..1188725 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "express-template", + "description": "Centralizer and library of API connections to multiple databases.", + "version": "3.0.0", + "license": "ISC", + "scripts": { + "tsc": "yarn", + "dev": "ts-node-dev src/app.ts", + "build": "NODE_ENV=development&& tsc", + "start": "NODE_ENV=development&& node dist/app.js", + "build:prod": "NODE_ENV=production && tsc", + "start:prod": "NODE_ENV=production && node dist/app.js", + "lint": "eslint . --ext .js,.ts" + }, + "dependencies": { + "config": "^3.3.11", + "cors": "^2.8.5", + "express": "^4.18.2", + "nanoid": "3.3.7", + "v-functions": "git+https://2bdcc0300e0ed5ac01f9dcd51368f7ac74fdb85a@git.via-asesores.com/libraries/v-functions.git" + }, + "devDependencies": { + "@types/config": "^3.3.3", + "@types/express": "^4.17.21", + "@types/node": "^20.11.20", + "@types/ws": "^8.5.10", + "@typescript-eslint/eslint-plugin": "^6.4.0", + "@typescript-eslint/parser": "^7.0.1", + "eslint": "^8.0.1", + "ts-node-dev": "^2.0.0", + "typescript": "^5.3.3" + } +} diff --git a/src/api/controllers/testController.ts b/src/api/controllers/testController.ts new file mode 100644 index 0000000..51d3603 --- /dev/null +++ b/src/api/controllers/testController.ts @@ -0,0 +1,22 @@ +import { Request, Response } from 'express' + +const testController = { + get: (req: Request, res: Response): void => { + console.log('GET request to the test endpoint') + res.status(200).send('GET request to the test endpoint') + }, + post: (req: Request, res: Response): void => { + console.log('POST request to the test endpoint') + res.status(201).send('POST request to the test endpoint') + }, + put: (req: Request, res: Response): void => { + console.log('PUT request to the test endpoint') + res.status(200).send('PUT request to the test endpoint') + }, + delete: (req: Request, res: Response): void => { + console.log('DELETE request to the test endpoint') + res.status(200).send('DELETE request to the test endpoint') + } +} + +export default testController diff --git a/src/api/middlewares/globalMiddleware.ts b/src/api/middlewares/globalMiddleware.ts new file mode 100644 index 0000000..264f30c --- /dev/null +++ b/src/api/middlewares/globalMiddleware.ts @@ -0,0 +1,7 @@ +import { Request, Response, NextFunction } from 'express'; + +const globalMiddleware = (req: Request, res: Response, next: NextFunction): void => { + next(); +}; + +export default globalMiddleware; diff --git a/src/api/routes/testRoutes.ts b/src/api/routes/testRoutes.ts new file mode 100644 index 0000000..fdb6633 --- /dev/null +++ b/src/api/routes/testRoutes.ts @@ -0,0 +1,11 @@ +import { Router } from 'express' +import testController from '../controllers/testController' + +const router = Router() + +router.get('/', testController.get) +router.post('/', testController.post) +router.put('/', testController.put) +router.delete('/', testController.delete) + +export default router diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..416deb6 --- /dev/null +++ b/src/app.ts @@ -0,0 +1,20 @@ +import express, { Application } from 'express' +import { createServer } from 'http' +import testRouter from './api/routes/testRoutes' + +const app: Application = express() +const server = createServer(app) +const PORT: number = 9000 + +app.use(express.json()) + +// Rutas +app.use('/test', testRouter) + +const startServer = (): void => { + server.listen(PORT, () => { + console.log(`Server running on port ${PORT}`) + }) +} + +startServer() diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..d605f97 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,11 @@ +export type Weather = 'sunny' | 'rainy' | 'cloudy' | 'stormy'; +export type Visibility = 'great' | 'good' | 'ok' | 'poor'; + +export interface DiaryEntry { + id: number; + date: string; + weather: Weather; + visibility: Visibility; + comment: string; +} +export type NonSensitiveDiaryEntry = Omit; diff --git a/src/utils/environment.js b/src/utils/environment.js new file mode 100644 index 0000000..e69de29 diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d839f05 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "allowJs": true, + "target": "es5", + "module": "commonjs", + "outDir": "./dist", + "baseUrl": ".", + "paths": { + "@/*": ["./src/*"] + }, + "strict": false, + "noUnusedLocals": false, + "noUnusedParameters": false, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "esModuleInterop": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "strictNullChecks": true, + "forceConsistentCasingInFileNames": true, + }, + "include": ["src/**/*"] +} \ No newline at end of file