Skip to content

Commit 2ad571e

Browse files
authored
Express server optimization (#16)
* optimization wip * express-server done
1 parent 6a78353 commit 2ad571e

File tree

20 files changed

+138
-101
lines changed

20 files changed

+138
-101
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can clone the full repo and keep only the packages you need in your monorepo
1010
- [React](https://reactjs.org/), [NestJs](https://nestjs.com/), [ExpressJS](https://expressjs.com/), [NestJS](https://nestjs.com/)
1111
- 100% [Typescript](https://www.typescriptlang.org/)
1212
- [Prettier](https://prettier.io/) and [Eslint](https://eslint.org/) setup alongside `pre-commit` hook.
13-
- [Mui v6](https://mui.com/) alongside theme change preconfigured.
13+
- [Mui v7](https://mui.com/) alongside theme change preconfigured.
1414
- [Dockerize](https://docs.docker.com/) images
1515
- Github Actions to build apps and publish their docker images
1616

apps/express-server/src/app-constants/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/express-server/src/app.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import express, {
44
type Response
55
} from 'express';
66
import cors from 'cors';
7-
import { ENV_VARS } from '@/app-constants';
7+
import { ENV_CONFIG } from '@/constants';
88
import { requestLogger } from '@/middleware';
9-
import * as Routes from '@/routes';
9+
import { routesList } from '@/routes';
10+
import { sendErrorResponse } from '@/utils';
1011

1112
const app: Express = express();
1213

@@ -17,15 +18,22 @@ app.use(cors());
1718
app.use(requestLogger);
1819

1920
app.get('/', (_: Request, response: Response) => {
20-
response.status(200).send(`ENV: ${ENV_VARS.env} - Api is up & running!!!`);
21+
response.status(200).json({
22+
env: ENV_CONFIG.env,
23+
message: 'Api is up & running!!!'
24+
});
2125
});
2226

23-
app.use('/api/auth', Routes.authRouter);
27+
routesList.forEach(route => app.use(route.path, route.router));
2428

2529
/* 404 Handler - To be written at last */
2630
app.get('*', (req: Request, response: Response) => {
27-
const notFoundMsg = `Not Found - "${req.originalUrl}"`;
28-
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+
});
2937
});
3038

3139
export default app;

apps/express-server/src/app-constants/env_vars.ts renamed to apps/express-server/src/constants/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const env = process.env;
77

8-
export const ENV_VARS = Object.freeze({
8+
export const ENV_CONFIG = Object.freeze({
99
env: env.NODE_ENV ?? 'development',
1010
port: env.PORT ?? 8000
1111
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './environment';

apps/express-server/src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,27 @@
1717
import 'dotenv/config';
1818
import os from 'os';
1919
import { createServer } from 'node:http';
20-
import { ENV_VARS } from '@/app-constants';
20+
import { ENV_CONFIG } from '@/constants';
2121
import { winstonLogger } from '@/middleware';
2222
import app from './app';
2323

2424
const hostName = os.hostname();
25-
const port = ENV_VARS.port;
25+
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} ⚡️ ] - 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/middleware/winston-logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/restrict-template-expressions */
22

33
import { createLogger, addColors, format, transports } from 'winston';
4-
import { ENV_VARS } from '@/app-constants';
4+
import { ENV_CONFIG } from '@/constants';
55

66
const { combine, timestamp, printf } = format;
77

@@ -72,7 +72,7 @@ addColors(customLevels.colors);
7272
* `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
7373
*/
7474

75-
if (ENV_VARS.env !== 'production') {
75+
if (ENV_CONFIG.env !== 'production') {
7676
winstonLogger.add(
7777
new transports.Console({ format: format.colorize({ all: true }) })
7878
);

0 commit comments

Comments
 (0)