Skip to content

Commit abbd70b

Browse files
Merge pull request #5 from hms-int/dev-gautam
refactor code
2 parents 2fdbcbf + 14abd26 commit abbd70b

4 files changed

Lines changed: 26 additions & 13 deletions

File tree

server.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import 'dotenv/config';
22
import app from './src/app.js';
33
import connectDB from './src/config/db.js';
44

5-
connectDB();
6-
75
const PORT = process.env.PORT || 5000;
86

9-
app.listen(PORT, () => {
10-
console.log(`Server running on port ${PORT}`);
11-
});
7+
connectDB().then(() => {
8+
app.listen(PORT, () => {
9+
console.log(`Server running on port ${PORT}`);
10+
});
11+
}).catch((error) => {
12+
console.log(`Error: ${error}`);
13+
});

src/app.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ import appointmentRoutes from './routes/appointment.js';
1919
const app = express();
2020

2121
// Global middleware
22-
app.use(cors());
22+
app.use(cors({
23+
origin: "*",
24+
credentials: true
25+
}));
2326
app.use(express.json());
2427
app.use(morgan('dev'));
2528

26-
app.get('/', (req, res) => {
27-
res.json({ ok: true });
29+
app.get('/', (_req, res) => {
30+
res.status(200).json({
31+
message: "Backend is online"
32+
})
2833
});
2934

3035
app.use('/api/auth', authRoutes);
@@ -41,10 +46,10 @@ app.use('/api/patients', patientRoutes);
4146
app.use('/api/departments', deptRoutes);
4247
app.use('/api/appointments', appointmentRoutes);
4348

44-
app.use((err, req, res, next) => {
49+
app.use((err, _req, res, _next) => {
4550
console.error(err);
4651
res.status(err.status || 500).json({
47-
error: err.message || 'Server error'
52+
error: err.message || 'Internal Server Error'
4853
});
4954
});
5055

src/controllers/authcontroller.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ export const login = async (req, res) => {
66
try {
77
const { role, email, password } = req.body;
88

9+
if (!role || !email || !password) {
10+
return res.status(400).json({
11+
success: false,
12+
message: "Invalid Request",
13+
});
14+
}
15+
916
const user = await User.findOne({
1017
email: { $regex: new RegExp(`^${email}$`, 'i') },
1118
role: { $regex: new RegExp(`^${role}$`, 'i') },
@@ -48,4 +55,4 @@ export const login = async (req, res) => {
4855
console.error('Login error:', error);
4956
res.status(500).json({ success: false, message: 'Server error' });
5057
}
51-
};
58+
};

src/middleware/authmiddleware.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import jwt from 'jsonwebtoken';
2-
import User from '../models/User.js';
32

43
const protect = (req, res, next) => {
54
let token;
@@ -44,4 +43,4 @@ const authorize = (...roles) => {
4443
};
4544

4645

47-
export { protect, authorize };
46+
export { protect, authorize };

0 commit comments

Comments
 (0)