forked from Yashgabani845/hiring-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 1.37 KB
/
server.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
65
// server.js
const express = require('express');
const cors = require('cors');
const app = express();
const connectDB = require('./db');
const Userdb = require('./models');
const port = 3001;
// Connect to the database
connectDB();
// Middleware to parse JSON bodies
app.use(express.json());
// Enable CORS
app.use(cors());
// Home route
app.get("/", (req, res) => {
res.send("<h1>Home page of a Job portal </h1>")
});
// Create a new job
app.post("/api/jobs", async (req, res) => {
const { title,
company,
location,
description, salary } = req.body;
try {
const newJob = new Userdb({
title,
company,
location,
description,
salary
});
await newJob.save();
res.status(201).json(newJob);
} catch (error) {
console.error('Error creating job:', error);
res.status(500).json(
{ message: 'Server Error' }
);
}
});
// Get all jobs
app.get("/api/jobs", async (req, res) => {
try {
const jobs = await Userdb.find();
res.json(jobs);
} catch (error) {
console.error('Error getting jobs:', error);
res.status(500).json(
{ message: 'Server Error' }
);
}
});
// Start the server
app.listen(port, () => {
console.log(
`Server is started at port no ${port}`);
});