Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from 'dotenv';
dotenv.config();

const isProduction = process.env.NODE_ENV === 'production'
export const isProduction = process.env.NODE_ENV === 'production'

// MongoDB URI
export const MONGOURI = isProduction
Expand Down
16 changes: 13 additions & 3 deletions src/controllers/createLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ interface getLogsRequest extends Request {

const createLog = async (req: getLogsRequest, res: Response) => {
const { user, logType, questionId, quizId } = req.body

if (!mongoose.Types.ObjectId.isValid(quizId)) {
return res.status(400).json({
message: 'Invalid quizId',
})
}

try {
const log = await LogModel.create({
const log = new LogModel({
userId: user.userId,
logType,
...(questionId && { questionId }),
quizId,
quizId: new mongoose.Types.ObjectId(quizId),
})

await log.save()

return res.status(200).json({
message: 'Log created successfully',
log,
})
} catch (error: unknown) {
console.log(error)
console.error(error)
return res.status(500).json({
message: 'Error in creating log',
error,
Expand Down
10 changes: 7 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import morgan from 'morgan'
import mongoSanitize from 'express-mongo-sanitize'
import logger from '@utils/logger'
import profilePageRouter from '@routers/profilePage'
import { FRONTEND_URL } from 'config'
import { isProduction, FRONTEND_URL } from 'config'
import cluster from 'cluster'
import os from 'os'

const numInstances = os.cpus().length

if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`)
if (isProduction && cluster.isMaster) {
console.log(`Master ${process.pid} is running in production environment`)

// Fork workers.
for (let i = 0; i < numInstances; i++) {
Expand Down Expand Up @@ -84,6 +84,10 @@ if (cluster.isMaster) {
app.get('/', (req: Request, res: Response) => {
res.send('Express + TypeScript Server')
})

app.get('/health', (req: Request, res: Response) => {
res.status(200).send('Server is up and running')
})

server.listen(port, () => {
logger.silly(`⚡️[server]: Server is running at http://localhost:${port}`)
Expand Down