You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
47 lines
1.2 KiB
const express = require('express');
|
|
const app = express();
|
|
const DataStore = require('nedb');
|
|
const { response } = require('express');
|
|
const PORT = process.env.PORT || 5000;
|
|
|
|
app
|
|
.use(express.static(__dirname + '/public'))
|
|
.use(express.json())
|
|
.get('/', (req, res) => res.render('public/index'))
|
|
.listen(PORT, () => console.log(`Listening on ${PORT}`))
|
|
|
|
|
|
const database = new DataStore('data.db');
|
|
database.loadDatabase();
|
|
|
|
app.get('/student', (req, res) => {
|
|
database.find({}, (err, data) => {
|
|
res.json(data);
|
|
});
|
|
});
|
|
|
|
app.post('/student', (req, res) => {
|
|
database.insert(req.body, function(err, newStudent){
|
|
if (err){
|
|
res.status(500).json(err);
|
|
}
|
|
console.log("new student added", newStudent);
|
|
res.json(newStudent);
|
|
});
|
|
});
|
|
|
|
app.delete('/student', (req, res) => {
|
|
console.log(req.body)
|
|
database.remove({name: req.body.name}, {}, function(err, newStudent){
|
|
if (err){
|
|
res.status(500).json(err);
|
|
}
|
|
console.log("student removed", newStudent);
|
|
// database.persistence.compactDatafile()
|
|
return res.send("Stdent has been removed");
|
|
|
|
});
|
|
// database.persistence.compactDatafile()
|
|
res.send("Seems like we don't have anything to delete");
|
|
});
|