-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (51 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const path = require('path');
const morgan = require('morgan')
const mongoose = require('mongoose');
const userRoutes = require('./routes/usersRoutes')
const cropsRouts = require('./routes/cropsRouter')
const farmRouts = require('./routes/farmRouter')
const authRoutes = require('./routes/auth')
const fertlizersRoutes = require('./routes/fertilizerRouter')
const equipmentRoutes = require('./routes/equipmentRouter')
const medicinesRoutes = require('./routes/medicinesRoutes')
const cors = require('cors')
require('./db')
app.use(cors())
// Middleware to check API key
const apiKeyMiddleware = (req, res, next) => {
const apiKey = req.headers['api-key'];
console.log("🚀 ~ apiKeyMiddleware ~ apiKey:", apiKey)
if (!apiKey || apiKey !== process.env.API_KEY) {
return res.status(401).json({ message: 'Unauthorized' });
}
next();
};
app.use(apiKeyMiddleware);
app.use(express.json());
app.use(express.urlencoded())
app.use(morgan('tiny'))
app.use('/api/users', userRoutes);
app.use('/api/crops', cropsRouts);
app.use('/api/farm', farmRouts);
app.use('/api/fertlizers', fertlizersRoutes);
app.use('/api/equipments', equipmentRoutes);
app.use('/api/medicines', medicinesRoutes);
app.use('/api/auth', authRoutes)
app.get('/', (req, res) => {
res.send(path.join(__dirname, 'index.html'));
});
app.use((err,req,res,next)=>{
const statusCode = err.statusCode || 500;
res.status(statusCode).send({
status:statusCode,
message: err?.message || 'internal server error',
errors: err?.errors || []
})
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});