Skip to content

Commit e6eb660

Browse files
committed
express-server done
1 parent 6e5a656 commit e6eb660

File tree

13 files changed

+106
-94
lines changed

13 files changed

+106
-94
lines changed

apps/express-server/src/app.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cors from 'cors';
77
import { ENV_CONFIG } from '@/constants';
88
import { requestLogger } from '@/middleware';
99
import { routesList } from '@/routes';
10+
import { sendErrorResponse } from '@/utils';
1011

1112
const app: Express = express();
1213

@@ -23,14 +24,16 @@ app.get('/', (_: Request, response: Response) => {
2324
});
2425
});
2526

26-
routesList.forEach(route => {
27-
app.use(route.path, route.router);
28-
});
27+
routesList.forEach(route => app.use(route.path, route.router));
2928

3029
/* 404 Handler - To be written at last */
3130
app.get('*', (req: Request, response: Response) => {
32-
const notFoundMsg = `Not Found - "${req.originalUrl}"`;
33-
response.status(404).send(notFoundMsg);
31+
const notFoundError = `No route exists for this endpoint: "${req.originalUrl}"`;
32+
return sendErrorResponse(response, {
33+
statusCode: 404,
34+
message: '404 - Not Found',
35+
error: notFoundError
36+
});
3437
});
3538

3639
export default app;

apps/express-server/src/index.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@ const hostName = os.hostname();
2525
const { port, env } = ENV_CONFIG;
2626

2727
function bootstrap() {
28-
/* DB Connection Logic */
29-
// try {
30-
// await mongoose.connect(db_connection_string, { autoIndex: true });
31-
// console_log('Connected to DATABASE', `${db_name}@${db_url}`);
32-
// } catch (err) {
33-
// console.log(chalk.red('⚠ Error connecting to the Database ⚠'));
34-
// console.log(err);
35-
// process.exit(1);
36-
// }
3728

38-
const server = createServer(app);
29+
/**
30+
* You can write any custom logic here, like connecting to the
31+
* database. Refer this snippet to connect to MongoDB, Postgres
32+
* or MySQL database.
33+
*
34+
* https://github.com/nishkohli96/client-server-libs/blob/main/apps/express-server/src/index.ts#L171
35+
*/
3936

37+
const server = createServer(app);
4038
server.listen(port, () => {
4139
winstonLogger.info(
42-
`[ ⚡️ ${hostName}@${env} ⚡️ ] - Server running on port ${port}`
40+
`[⚡️ ${hostName}@${env} ⚡️] - Server running on port ${port}`
4341
);
4442
});
4543
}

apps/express-server/src/middleware/guard.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './guard';
21
export * from './request-logger';
32
export * from './winston-logger';

apps/express-server/src/middleware/request-logger.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,3 @@ export function requestLogger(
1616
});
1717
next();
1818
}
19-
20-
export function printSuccessMsg(msg: string): void {
21-
winstonLogger.info(`✅ SUCCESS - ${msg}`);
22-
}
23-
24-
export function printError(error: unknown): void {
25-
winstonLogger.error(
26-
`⚠ ERROR - ${error instanceof Error ? error.message : JSON.stringify(error)}`
27-
);
28-
}
29-
30-
export function errorLogger(res: Response, error: unknown) {
31-
const err = JSON.stringify(error);
32-
printError(error);
33-
res.status(500).send(err);
34-
}

apps/express-server/src/routes/auth/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Router, type Request, type Response } from 'express';
1+
import { Router, type Response } from 'express';
22
import authService from './service';
33
import type * as AuthTypes from './types';
44

@@ -12,7 +12,7 @@ authRouter.get('/test', function printHello(_, res: Response) {
1212
authRouter.post(
1313
'/login',
1414
function loginUser(
15-
req: Request<object, object, AuthTypes.UserLoginBody>,
15+
req: AuthTypes.UserLoginRequest,
1616
res: Response
1717
) {
1818
const { email, password } = req.body;
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import type { Response } from 'express';
2+
import { sendErrorResponse } from '@/utils';
23

34
class AuthService {
45
loginUser(res: Response, email: string, password: string) {
56
try {
6-
res
7+
return res
78
.status(200)
89
.send({
910
email,
1011
password
1112
})
1213
.end();
1314
} catch (error) {
14-
res.status(500).send(`Internal Server Error: ${JSON.stringify(error)}`);
15+
return sendErrorResponse(res, { error });
1516
}
16-
res.end();
1717
}
1818
}
1919

20-
export default new AuthService();
20+
const authService = new AuthService();
21+
export default authService;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { type Request } from 'express';
12
export interface UserLoginBody {
23
email: string;
34
password: string;
45
}
6+
7+
export type UserLoginRequest = Request<object, object, UserLoginBody>;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { type Router} from 'express';
1+
import { type Router } from 'express';
22
import { authRouter } from './auth/controller';
33

44
type RouteInfo = {
55
path: string;
66
router: Router;
7-
}
7+
};
88

99
function generatePrefix(routeName: string) {
10-
return `/api${routeName}`;
10+
return `/api${routeName}`;
1111
}
1212

1313
const routesList: RouteInfo[] = [
1414
{
15-
path: generatePrefix('/auth'),
16-
router: authRouter
17-
}
15+
path: generatePrefix('/auth'),
16+
router: authRouter
17+
}
1818
];
1919

2020
export { routesList };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './printObject';
2+
export * from './response';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Winston logger expects string message by default.
3+
* If you pass an object directly (like your response),
4+
* Winston doesn't automatically serialize it.
5+
*
6+
* JSON.stringify's 2nd and 3rd args:
7+
* - replacer (Optional): A function or array to control
8+
* which properties are included in the output. Usually
9+
* null if you want to include everything. Specify only
10+
* those keys in an array which you want to log.
11+
* - space (Optional): A number or string to control spacing
12+
* or indentation. 2 means 2 spaces of indentation per level
13+
* (for pretty-printing).
14+
*/
15+
export function printObject(obj: unknown): string {
16+
return JSON.stringify(obj, null, 2);
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Move this in types/request folder if you want to
3+
* use this code snippet.
4+
*/
5+
type PaginationConfig = {
6+
records_per_page: number;
7+
page: number;
8+
};
9+
10+
export const DefaultPaginationOptions: PaginationConfig = {
11+
records_per_page: 10,
12+
page: 1
13+
};
14+
15+
export function getPaginationParams(
16+
page?: string,
17+
records_per_page?: string
18+
): PaginationConfig {
19+
return {
20+
records_per_page: records_per_page
21+
? Number(records_per_page)
22+
: DefaultPaginationOptions.records_per_page,
23+
page: page ? Number(page) : DefaultPaginationOptions.page
24+
};
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { winstonLogger } from '@/middleware';
2+
import type { Response } from 'express';
3+
4+
type ErrorResponseOptions = {
5+
error: unknown;
6+
message?: string;
7+
statusCode?: number;
8+
};
9+
10+
export function sendErrorResponse(
11+
res: Response,
12+
{
13+
error,
14+
message = 'Internal Server Error',
15+
statusCode = 500,
16+
}: ErrorResponseOptions
17+
) {
18+
const errorMessage = error instanceof Error
19+
? error.message
20+
: JSON.stringify(error);
21+
22+
winstonLogger.error(`⚠ ERROR - ${errorMessage}`);
23+
return res.status(statusCode).json({
24+
success: false,
25+
status: statusCode,
26+
message,
27+
error: errorMessage,
28+
});
29+
}

0 commit comments

Comments
 (0)