Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CORS error and refactor server configuration #44

Merged
merged 58 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
0d959b4
update README.md
ballyalley-o Feb 25, 2024
5ad4e43
Add .env.sample file with default configuration
ballyalley-o Feb 28, 2024
241f78a
Update environment variable NODE_ENV to production and fix typo in cm…
ballyalley-o Feb 28, 2024
bab8b8b
Fix npm start command
ballyalley-o Feb 28, 2024
daa9d66
Add start.sh script to build and start the application
ballyalley-o Feb 28, 2024
ef05cb4
Update good-logs package to version 1.2.2
ballyalley-o Mar 1, 2024
e8a7957
Add typeRoots to tsconfig.json
ballyalley-o Mar 1, 2024
ded7bf9
Add import for path module and update serverRouter method
ballyalley-o Mar 1, 2024
37c48ba
Fix unhandled rejection error in server configuration
ballyalley-o Mar 1, 2024
3efb5a3
Add import statement for path module and assign it to a class property
ballyalley-o Mar 1, 2024
e6cac8f
Add ALREADY_EXISTS response message
ballyalley-o Mar 1, 2024
d6d4fe5
Add UnhandledRejection enum value
ballyalley-o Mar 1, 2024
b1b080c
Add PUBLIC constant to PathParam enum
ballyalley-o Mar 1, 2024
c754931
Add enum for request body
ballyalley-o Mar 1, 2024
8f14507
Add validation for email and username in register method
ballyalley-o Mar 1, 2024
8703d2a
Add colored logging and use good-logs library
ballyalley-o Mar 1, 2024
1e69e22
Remove unique constraint from database indexes
ballyalley-o Mar 1, 2024
49615e8
configured for ssh
ballyalley-o Mar 1, 2024
82657f9
Update favicon link in index.html
ballyalley-o Mar 1, 2024
b06a4a5
Remove unnecessary line in server.ts
ballyalley-o Mar 1, 2024
172a959
Fix duplicate email and username validation in AuthController
ballyalley-o Mar 1, 2024
856b227
Add email and username validation in UserController
ballyalley-o Mar 1, 2024
cc53e97
Add ENTITY_EXISTS response message
ballyalley-o Mar 1, 2024
3805cd4
Update ENTITY_EXISTS message in response constant
ballyalley-o Mar 1, 2024
580d56d
Add code to handle duplicate entity error
ballyalley-o Mar 1, 2024
131d8b2
bumped package.json version and rename docs script
ballyalley-o Mar 1, 2024
c3c1046
Merge branch 'main' of https://github.com/ballyalley-o/tccp-server
ballyalley-o Mar 1, 2024
faa0868
Update start.sh script to use npm start instead of pm2 start
ballyalley-o Mar 1, 2024
78a81cc
updated app title
ballyalley-o Mar 1, 2024
713ef2c
Updated App Title
ballyalley-o Mar 1, 2024
962466b
Update API endpoint names and add new endpoint for uploading an avatar
ballyalley-o Mar 2, 2024
bebf49b
Merge remote-tracking branch 'origin'
ballyalley-o Mar 2, 2024
89ca5b0
Add start.sh to .gitignore
ballyalley-o Mar 24, 2024
0c282e9
Update good-logs package version to 1.2.3
ballyalley-o Mar 24, 2024
c4f3a62
cors origins added, styles added API and file upload configuration,
ballyalley-o Mar 24, 2024
1218bed
Add sample.env file with fake environment variables
ballyalley-o Mar 24, 2024
32d4144
Move header to middleware #43: moved header to middleware to use new…
ballyalley-o Mar 24, 2024
2bf6e5d
Remove unused import in config/index.ts
ballyalley-o Mar 24, 2024
a89ffa4
Move header to middleware #43: Refactor server configuration and midd…
ballyalley-o Mar 24, 2024
86119bc
[BUG: Server] Cors error #40: 🌈 style(Refactor code and fix formattin…
ballyalley-o Mar 24, 2024
d36e030
🌈 style(Refactor code and fix formatting issues):
ballyalley-o Mar 24, 2024
8b25a7f
🌈 style(Refactor code and fix formatting issues & removed logging for…
ballyalley-o Mar 24, 2024
72ec8c1
🌈 style(Refactor course controller formatting):
ballyalley-o Mar 24, 2024
8bffe59
🌈 style(Refactor FeedbackController formatting):
ballyalley-o Mar 24, 2024
dc32f2a
Refactor debugger decorator to use good-logs * added styles 🌈
ballyalley-o Mar 24, 2024
0ebcef0
🌈 style(Refactor LogRequest code formatting):
ballyalley-o Mar 24, 2024
d3f947a
🌈 style(Refactor middleware and decorator code formatting):
ballyalley-o Mar 24, 2024
c73351b
[BUG FIX: Server] Cors error #40 Add CORS configuration middleware
ballyalley-o Mar 24, 2024
6cbc460
🌈 style(Refactor error-handler middleware code formatting):
ballyalley-o Mar 24, 2024
1245900
Remove logger middleware, replaced by good-logs
ballyalley-o Mar 24, 2024
6c6c317
🌈 style(Refactor BootcampSchema pre-remove hook code styling):
ballyalley-o Mar 24, 2024
5ee0e05
🌈 style(Refactor CourseSchema code styling):
ballyalley-o Mar 24, 2024
8119a50
🌈 style(Refactor bootcamp routes code styling):
ballyalley-o Mar 24, 2024
6ed05d6
🌈 style(Refactor sendEmail function code styling):
ballyalley-o Mar 24, 2024
544fb14
init .prettierrc configuration file
ballyalley-o Mar 24, 2024
7e0c40f
[ Fix ] upload photo field for bootcamp #41: Update photo path in Boo…
ballyalley-o Mar 24, 2024
a844bc7
Limit the description #42: Update MAX_LENGTH_DESCRIPTION in SCHEMA enum
ballyalley-o Mar 24, 2024
a3156f6
Limit the description #42: Fix code formatting and syntax issues in B…
ballyalley-o Mar 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
tsconfig.tsbuildinfo

updtlog.sh
start.sh

public/upload/tccp-*.*
public/avatar/tccp-*.*
20 changes: 20 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"printWidth": 150,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": true,
"arrowParens": "always",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "auto",
"embeddedLanguageFormatting": "auto"
}
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,3 @@ How was each feature of the application tested?
Backend been tested well.
How did you handle edge cases?
Will still work on that. Did not get to its final version.

### Implementation

Deploy server on digital ocean so it will be available whenever, wherever.

### End-to-end solution
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tccp-server",
"version": "1.0.0",
"version": "1.0.3",
"description": "Server for The CodeCoach Projct ",
"main": "app.ts",
"type": "module",
Expand All @@ -10,7 +10,7 @@
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
"sim": "node dist/sim.js -i",
"destroy": "node dist/sim.js -d",
"docs": "typedoc --out docs '*/*.ts'",
"doc": "typedoc --out docs '*/*.ts'",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down Expand Up @@ -48,7 +48,7 @@
"express-fileupload": "^1.4.3",
"express-mongo-sanitize": "^2.2.0",
"express-rate-limit": "^7.1.5",
"good-logs": "^1.1.0",
"good-logs": "^1.2.3",
"helmet": "^7.1.0",
"hpp": "^0.2.3",
"jsonwebtoken": "^9.0.2",
Expand Down
70 changes: 59 additions & 11 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta http-equiv=x-ua-compatible content="ie=edge">
<meta name=viewport content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="./index.css">
<link rel="icon" href="./favicon64x64.ico" type="image/x-icon">
<title>TCCP [SERVER]</title>
<link rel="icon" href="/asset/favicon64x64.ico" type="image/x-icon">
<title>The Code Coach Projct</title>
<div class=container-fluid>
<div class=row>
<div class="col-md-12 collection-intro">
Expand Down Expand Up @@ -53,20 +53,21 @@ <h1 style="margin-right: auto;"> the code coach projct [SERVER]</h1>
<li><strong><a href=#User>User</a></strong>
<ul>
<li><a class=endpoint_menu href=#jump-User-GetUser>Get User</a>
<li><a class=endpoint_menu href=#jump-User-GetAUser>Get A User</a>
<li><a class=endpoint_menu href=#jump-User-CreateAUser>Create A User</a>
<li><a class=endpoint_menu href=#jump-User-UpdateAUser>Update A User</a>
<li><a class=endpoint_menu href=#jump-User-DeleteAUser>Delete A User</a>
<li><a class=endpoint_menu href=#jump-User-GetAUser>Get a User</a>
<li><a class=endpoint_menu href=#jump-User-CreateAUser>Create a User</a>
<li><a class=endpoint_menu href=#jump-User-UpdateAUser>Update a User</a>
<li><a class=endpoint_menu href=#jump-User-DeleteAUser>Delete a User</a>
<li><a class=endpoint_menu href=#jump-User-UploadanAvatar>Upload an Avatar</a>
</ul>
<li><strong><a href=#Feedback>Feedback</a></strong>
<ul>
<li><a class=endpoint_menu href=#jump-Feedback-GetAllFeedback>Get All Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-GetFeedbacksforBootcamp>Get Feedbacks for
Bootcamp</a>
<li><a class=endpoint_menu href=#jump-Feedback-GetAFeedback>Get A Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-AddAFeedback>Add A Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-UpdateAFeedback>Update A Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-DeleteAFeedback>Delete A Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-GetAFeedback>Get a Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-AddAFeedback>Add a Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-UpdateAFeedback>Update a Feedback</a>
<li><a class=endpoint_menu href=#jump-Feedback-DeleteAFeedback>Delete a Feedback</a>
</ul>
</ul>
</div>
Expand Down Expand Up @@ -1116,6 +1117,53 @@ <h5 class="label label-info">URL Variables</h5>
</div>
</div>
</div>
<div class="panel panel-warning" id=jump-User-UploadanAvatar>
<div class=panel-heading><small
class="pull-right text-muted">formdata</small>
<a class=text-warning data-toggle=collapse
data-parent=#accordion-User
href=#collapse-User-UploadanAvatar>
<h4 class=panel-title><span
class="glyphicon glyphicon-plus"></span><strong>Upload an
Avatar</strong> &nbsp;|&nbsp;
<strong class="request-method border-warning">PUT</strong>
&nbsp;
</h4>
</a></div>
<div id=collapse-User-UploadanAvatar class="panel-collapse collapse">
<div class=panel-body>
<h5 class="label label-default">Description</h5><br><small>
<p>Request route for Uploading an Avatar
</small><br><br>
<h5 class="label label-info">URL Variables</h5>
<table class="table table-hover">
<thead>
<tr>
<th>Key
<th>Value
<th>Description
<tbody>
<tr>
<td>id
<td>5d725a1b7b292f5f8cefb098
<td>
</table>
<h5 class="label label-primary">Body</h5>
<table class="table table-hover">
<thead>
<tr>
<th>Key
<th>Value
<th>Description
<tbody>
<tr>
<td>photo
<td>
<td>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -6448,4 +6496,4 @@ <h5 class="label label-info">URL Variables</h5>
<div class="span12 text-center"><span data-toggle=tooltip
title="If the application help you, please feel free to give a star to the project in github. Your star inspire me to work more on open-source projects like this!"> api.thecodecoachprojct.io</span></div>
</div>
</footer>
</footer>
43 changes: 43 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
API_HOST=http://localhost:{PORT}
API_URL=http://localhost:{PORT}/api
CLIENT_URL=https://fakeprojct.com
CORS_ALLOWED_ORIGIN=https://fakeprojct.com,http://localhost:5173,http://localhost:5174
API_VERSION=/v1.0.3
PORT=3003
DEV_CLIENT_PORT=5173
NODE_ENV=production

# Database
# db
DB_URI=mongodb+srv://fakeuser:[email protected]/FakeDB?retryWrites=true&w=majority
DB_NAME=FakeDB
DB_PROTOCOL=mongodb+srv://
DB_HOST=fakecluster.mongodb.net
DB_USER=fakeuser
DB_PW=fakepassword

# jwt
JWT_SECRET=fakeServer
JWT_EXP=30d
JWT_COOKIE_EXPIRE=

# mail - mailtrap
SMTP_HOST=fake.smtp.mailtrap.io
SMTP_PORT=2525
SMTP_EMAIL=fakeemail
SMTP_PASSWORD=fakepassword
[email protected]
FROM_NAME=FakeName

# rate limit
RATE_LIMIT=100

# neocoder
GEOCODER_PROVIDER=fakeprovider
GEOCODER_API_KEY=fakeapikey

# photo
FILE_UPLOAD_PATH=./public/upload
AVATAR_UPLOAD_PATH=./public/avatar
MAX_FILE_UPLOAD=1000000
MAX_AVATAR_UPLOAD=500000
11 changes: 7 additions & 4 deletions src/app-router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import express, { Router, Request, Response } from 'express'
import { GLOBAL } from '@config'
import { PathDir, RESPONSE } from '@constant'
Expand All @@ -18,10 +19,12 @@ class AppRouter {

static serverRouter() {
if (GLOBAL.ENV === ENV) {
this._router.use(express.static(PathDir.BUILD_LOC))
this._router?.get(PathParam.ALL, (req: Request, res: Response) =>
res.sendFile(PathDir.BUILD_VIEW)
)
// when frontend is ready
// this._router.use(express.static(PathDir.BUILD_LOC))
// this._router?.get(PathParam.ALL, (req: Request, res: Response) =>
// res.sendFile(PathDir.BUILD_VIEW)
// )
this._router?.get(PathDir.API_ROOT, RESPONSE.server)
} else {
this._router?.get(PathDir.API_ROOT, RESPONSE.server)
}
Expand Down
23 changes: 8 additions & 15 deletions src/config/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ dotenv.config()
const GLOBAL = {
APP_NAME: 'The CodeCoach Projct',
APP_SERVER_NAME: 'tccp-server',
API_HOST: conNex(
process.env.API_HOST?.replace('{PORT}', process.env.PORT ?? '') || ''
),
API_URL: conNex(
process.env.API_URL?.replace('{PORT}', process.env.PORT ?? '') || '',
process.env.API_VERSION || ''
),
API_HOST: conNex(process.env.API_HOST?.replace('{PORT}', process.env.PORT ?? '') || ''),
API_URL: conNex(process.env.API_URL?.replace('{PORT}', process.env.PORT ?? '') || '', process.env.API_VERSION || ''),
CLIENT_DEV_URL: conNex(process.env.API_HOST?.replace('{PORT}', process.env.DEV_CLIENT_PORT ?? '') || ''),
CORS_ALLOWED_ORIGIN: process.env.CORS_ALLOWED_ORIGIN?.split(',') || [],
API_VERSION: process.env.API_VERSION || '',
PORT: process.env.PORT || 3005,
ENV: process.env.NODE_ENV || '',
Expand All @@ -33,15 +30,11 @@ const GLOBAL = {
MAX_FILE_UPLOAD: process.env.MAX_FILE_UPLOAD || NumKey.ONE_MB,
PHOTO_UPLOAD_PATH: process.env.PHOTO_UPLOAD_PATH,

PHOTO_FILENAME: (bootcampId: ObjectId, name: string) =>
`tccp-${bootcampId}${path.parse(name).ext}`,
AVATAR_FILENAME: (userId: ObjectId, name: string) =>
`tccp-av-${userId}${path.parse(name).ext}`,
PHOTO_FILENAME: (bootcampId: ObjectId, name: string) => `tccp-${bootcampId}${path.parse(name).ext}`,
AVATAR_FILENAME: (userId: ObjectId, name: string) => `tccp-av-${userId}${path.parse(name).ext}`,

PHOTO_UPLOAD_MV: (photo: any, cb: any) =>
photo.mv(`${process.env.FILE_UPLOAD_PATH}/${photo.name}`, cb),
AVATAR_UPLOAD_MV: (photo: any, cb: any) =>
photo.mv(`${process.env.AVATAR_UPLOAD_PATH}/${photo.name}`, cb),
PHOTO_UPLOAD_MV: (photo: any, cb: any) => photo.mv(`${process.env.FILE_UPLOAD_PATH}/${photo.name}`, cb),
AVATAR_UPLOAD_MV: (photo: any, cb: any) => photo.mv(`${process.env.AVATAR_UPLOAD_PATH}/${photo.name}`, cb),

// @mail - nodemailer - mailtrap
MAIL_FROM: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
Expand Down
17 changes: 0 additions & 17 deletions src/config/header.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path'
export { default as App } from '@config/server'
export { default as setHeader } from '@config/header'
export { default as connectDb } from '@config/db'
export { default as GLOBAL } from '@config/global'

Expand Down
Loading