forked from 123Balramkumar/practical-task-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (60 loc) · 1.7 KB
/
index.js
File metadata and controls
68 lines (60 loc) · 1.7 KB
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
65
66
67
68
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const port = 9000;
app.use(cors());
// a660b1e2-aade-4875-a2b6-d5ea2a7fd180
// hsshoquz
// balram_job
// rFzGWa52gQ5EHjjq
// Connect to MongoDB Atlas (replace the connection string with your own)
const MONGODB_URI = 'mongodb+srv://balram_job:rFzGWa52gQ5EHjjq@cluster0.ykxdiog.mongodb.net/?retryWrites=true&w=majority';
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('Connected to MongoDB');
})
.catch((err) => {
console.error('Error connecting to MongoDB:', err);
});
// Define a job schema
const jobSchema = new mongoose.Schema({
name: String,
location: String,
posted: String,
status: String,
applied: Number,
jobViews: Number,
daysLeft: Number,
premium: Boolean,
dateFormat: String,
});
// Create a job model based on the schema
const Job = mongoose.model('Job', jobSchema);
app.use(bodyParser.json());
// POST endpoint for creating a new job entry
app.post('/api/jobs/create', async (req, res) => {
const newJobData = req.body;
try {
const createdJob = await Job.create(newJobData);
res.status(201).json(createdJob);
} catch (err) {
res.status(500).json({ error: 'Failed to create the job entry' });
}
});
// GET endpoint to retrieve all job data
app.get('/api/jobs', async (req, res) => {
try {
const allJobs = await Job.find();
res.json(allJobs);
} catch (err) {
res.status(500).json({ error: 'Failed to get job data' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});