Advertisement
Grossos

app.js

Dec 3rd, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const { db } = require('./config/db-config');
  3. const { dbService } = require('./config/db-config');
  4. const dotenv = require('dotenv');
  5. const app = express();
  6. const cookie = require('cookie-parser');
  7. const cors = require('cors');
  8. // const { name } = require('ejs');
  9. const PORT = process.env.PORT || 5000;
  10. app.use('/js', express.static(__dirname + '/public/js'));
  11. app.use('/css', express.static(__dirname + '/public/css'));
  12. app.use(cookie());
  13. app.use(cors());
  14. app.use(express.urlencoded({ extended: false }));
  15. app.use(express.json());
  16. app.use('/', require('./controllers/pages'));
  17. app.use('/api', require('./controllers/auth'));
  18. app.use(require('./controllers/servers'));
  19. app.set('view engine', 'ejs');
  20. app.set('views', './views');
  21. db.connect((err) => {
  22.     if (err) throw err;
  23.     console.log('Database connected');
  24. });
  25. app.listen(process.env.PORT, () => console.log(`Listening on port ${PORT}`));
  26.  
  27. // create  
  28. app.post('/insert', (request, response) => {
  29.     console.log(request.body);
  30.     const { name } = request.body;
  31.     const dbS = dbService.getDbServiceInstance();
  32.  
  33.     const result = dbS.insertNewName(name);
  34.  
  35.     result
  36.         .then(data => response.json({ data: data }))
  37.         .catch(err => console.log(err));
  38.  
  39. });
  40.  
  41. // Create Read Function
  42. app.get('/getAll', (request, response) => {
  43.     console.log('Get');
  44.  
  45.     const dbS = dbService.getDbServiceInstance();
  46.     const result = dbS.getAllData();
  47.  
  48.     result
  49.         .then(data => response.json({ data: data }))
  50.         .catch(err => console.log(err));
  51.  
  52.     //   response.json({ success: true  });      
  53. });
  54.  
  55. // update
  56. app.patch('/update', (request, response) => { // :id ????
  57.     // console.log(request.body);
  58.     // console.log(request.headers);
  59.     // console.log(`METHOD:${request.method}`);
  60.     // console.log(`ID:${request.body.id}`);
  61.     // console.log(`NAME:${request.body.name}`);
  62.     const { id, name } = request.body;
  63.     const dbS = dbService.getDbServiceInstance();
  64.  
  65.     const result = dbS.updateNameById(id, name);
  66.  
  67.     result
  68.         .then(data => response.json({ success: data }))
  69.         .catch(err => console.log(err));
  70. });
  71.  
  72. // delete
  73.  
  74. app.delete('/delete/:id', (request, response) => {
  75.     const { id } = request.params;
  76.     const dbS = dbService.getDbServiceInstance();
  77.  
  78.     const result = dbS.deleteRowById(id);
  79.  
  80.     result
  81.         .then(data => response.json({ success: data }))
  82.         .catch(err => console.log(err));
  83.  
  84.     console.log(request.params);
  85. });
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement