-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (32 loc) · 1018 Bytes
/
server.js
File metadata and controls
32 lines (32 loc) · 1018 Bytes
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
const express = require('express');
const db = require('./db');
const httpServer = express();
httpServer.get('/', (req, res, next) => {
res.send("Hello from the other side")
})
httpServer.get('/students', (req, res, next) => {
let students = db.getAllStudents();
res.status(200).json(students);
})
httpServer.get('/student/:id', (req, res, next) => {
let studentId = req.params.id
try {
let student = db.getById(studentId);
res.status(200).json(student);
} catch (error) {
res.status(500).json({ status: "Error", message: 'Student not found' })
}
})
httpServer.use(express.json())
httpServer.post('/student', (req, res, next) => {
let student = req.body;
try {
let status = db.addStudent(student)
res.status(201).json(status);
} catch (error) {
res.status(500).json({ status: "Error", message: 'Something went wrong while adding student' })
}
})
httpServer.listen(3200, () => {
console.log("Server is running on 3200");
})