Skip to content

Commit

Permalink
express 설치 및 swagger 설치 (#1)
Browse files Browse the repository at this point in the history
* init expres

* chore: set swagger

* feat: reinstall dev packages

* refacotr: change middlewares
  • Loading branch information
rolled-potatoes authored Nov 19, 2023
1 parent 453e848 commit 9936793
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SERVER_PORT=
175 changes: 175 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# 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/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# 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

# 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.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
Binary file added bun.lockb
Binary file not shown.
10 changes: 10 additions & 0 deletions configs/swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'pi-don api doc',
version: '1.0.0',
},
},
apis: ['./src/**/*.swagger.js'],
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pi-don",
"module": "src/app.ts",
"type": "module",
"scripts": {
"start": "bun run src/app.ts",
"dev": "bun --hot run src/app.ts"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/swagger-jsdoc": "^6.0.3",
"@types/swagger-ui-express": "^4.1.6",
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"express": "^4.18.2",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0"
}
}
13 changes: 13 additions & 0 deletions src/app.middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import express from 'express';
import router from './routes';
import swaggerUi from 'swagger-ui-express';
import swaggerJSDoc from 'swagger-jsdoc';
import { options as swaggerJsdocOptions } from '../configs/swagger';

const swaggerSpec = swaggerJSDoc(swaggerJsdocOptions);

export default (app: express.Application) => {
app.use(express.urlencoded({ extended: false }));
app.use('/api', router);
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
};
14 changes: 14 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from 'express';
import middlewares from './app.middlewares';

const app = express();

middlewares(app);

app.use((_, res) => {
return res.status(404).send('페이지 낫 파운드');
});

app.listen(Bun.env.SERVER_PORT, () => {
console.log(`Bun server on : ${Bun.env.SERVER_PORT}`);
});
8 changes: 8 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from 'express';
import testRouter from './test';

const router = express.Router();

router.use('/test', testRouter);

export default router;
10 changes: 10 additions & 0 deletions src/routes/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from 'express';
import v1Router from './v1';
import v2Router from './v2';

const router = express.Router();

router.use('/v1', v1Router);
router.use('/v2', v2Router);

export default router;
12 changes: 12 additions & 0 deletions src/routes/test/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import express from 'express';

const router = express.Router();

router.get('', (_, res) => {
return res.status(200).json({
name : 'foo',
age : 'bar'
})
});

export default router;
27 changes: 27 additions & 0 deletions src/routes/test/v1/test.swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @swagger
* tags:
* - name: test
* description: test api doc
* @swagger
* /api/test/v1:
* get:
* tags: [test]
* summary: test v1 get
* description: test description
* responses:
* 200:
* description: Successful operation
* content:
* application/json:
* schema:
* properties:
* age:
* type: string
* example: bar
* description: bar
* name:
* type: string
* example: foo
* description : foo
*/
10 changes: 10 additions & 0 deletions src/routes/test/v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import express from 'express';

const router = express.Router();

router.get('', (_, res) => {
return res.send('test v2 router');
});

export default router;
20 changes: 20 additions & 0 deletions src/routes/test/v2/test.swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @swagger
* tags:
* - name: test
* description: test api doc
* @swagger
* /api/test/v2:
* get:
* tags: [test]
* summary: test v2 get
* description: test description
* responses:
* 200:
* description: Successful operation
* content:
* text/plain:
* schema:
* type: string
* example: hollo bun
*/
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types" // add Bun global
]
}
}

0 comments on commit 9936793

Please sign in to comment.