feat(init): 🎉 Initialize Express.js template

This commit is contained in:
Jorge Mario Arita Ramírez 2024-02-27 12:15:04 -06:00
parent 627fc7ec32
commit bbc8981148
16 changed files with 246 additions and 127 deletions

6
.dockerignore Normal file
View File

@ -0,0 +1,6 @@
node_modules
.vscode
.gitignore
README.md
.dockerignore
.git

41
.eslintrc.json Normal file
View File

@ -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"
}
}
]
}

139
.gitignore vendored
View File

@ -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
logs logs
*.log *.log
npm-debug.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

16
.vscode/settings.json vendored Normal file
View File

@ -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"
]
}

44
Dockerfile Normal file
View File

@ -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"]

0
config/default.json Normal file
View File

0
config/development.json Normal file
View File

0
config/production.json Normal file
View File

33
package.json Normal file
View File

@ -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"
}
}

View File

@ -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

View File

@ -0,0 +1,7 @@
import { Request, Response, NextFunction } from 'express';
const globalMiddleware = (req: Request, res: Response, next: NextFunction): void => {
next();
};
export default globalMiddleware;

View File

@ -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

20
src/app.ts Normal file
View File

@ -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()

11
src/types.d.ts vendored Normal file
View File

@ -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<DiaryEntry, 'comment'>;

0
src/utils/environment.js Normal file
View File

23
tsconfig.json Normal file
View File

@ -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/**/*"]
}