Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. 'use strict';
  2. const http = require('http');
  3. const express = require('express');
  4. const cors = require('cors');
  5.  
  6. const app = express();
  7.  
  8. const port = process.env.PORT || 4000;
  9. const host = process.env.HOST || 'localhost';
  10.  
  11. const palvelin = http.createServer(app);
  12.  
  13. const Tietovarasto = require('./autovarasto.js');
  14.  
  15. const optiot = {
  16. host: 'localhost',
  17. port: 3306,
  18. user: 'pihla',
  19. password: 'lhlPxtBq',
  20. database: 'mopokanta'
  21. };
  22.  
  23. const mopot = new Tietovarasto(optiot);
  24.  
  25. app.use(express.json());
  26. app.use(cors());
  27.  
  28. app.get('/', (req, res) => res.json({
  29. virhe: 'komento puuttuu'
  30. }));
  31.  
  32. app.get('/mopot', (req, res) => mopot.haeKaikki().then(tulos => res.json(tulos)).catch(virhe => res.json({
  33. virhe: virhe.message
  34. })));
  35. app.route('/mopot/:numero').get((req, res) => {
  36. const mopoId = req.params.numero;
  37. mopot.hae(mopoId).then(tulos => res.json(tulos)).catch(virhe => res.json({
  38. virhe: virhe.message
  39. }))
  40. }).delete((req, res) => {
  41. const mopoId = req.params.numero;
  42. mopot.poista(mopoId).then(tulos => res.json(tulos)).catch(virhe => res.json({
  43. virhe: virhe.message
  44. }))
  45. }).post((req, res) => {
  46. if (!req.body) res.json({
  47. virhe: 'ei löydy'
  48. });
  49. mopot.paivita(req.body).then(tulos => res.json(tulos)).catch(virhe => res.json({
  50. virhe: virhe.message
  51. }))
  52. }).put((req, res) => {
  53. if (!req.body) res.json({
  54. virhe: 'ei löydy'
  55. });
  56. mopot.lisaa(req.body).then(tulos => res.json(tulos)).catch(virhe => res.json({
  57. virhe: virhe.message
  58. }))
  59. });
  60.  
  61. app.all('*', (req, res) => res.json('resurssia ei löydy tai yksilöivä numero puuttuu'));
  62.  
  63. palvelin.listen(port, host, () => console.log(`Palvelin ${host} portissa ${port}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement