Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const http = require('http');
- const express = require('express');
- const cors = require('cors');
- const app = express();
- const port = process.env.PORT || 4000;
- const host = process.env.HOST || 'localhost';
- const palvelin = http.createServer(app);
- const Tietovarasto = require('./autovarasto.js');
- const optiot = {
- host: 'localhost',
- port: 3306,
- user: 'pihla',
- password: 'lhlPxtBq',
- database: 'mopokanta'
- };
- const mopot = new Tietovarasto(optiot);
- app.use(express.json());
- app.use(cors());
- app.get('/', (req, res) => res.json({
- virhe: 'komento puuttuu'
- }));
- app.get('/mopot', (req, res) => mopot.haeKaikki().then(tulos => res.json(tulos)).catch(virhe => res.json({
- virhe: virhe.message
- })));
- app.route('/mopot/:numero').get((req, res) => {
- const mopoId = req.params.numero;
- mopot.hae(mopoId).then(tulos => res.json(tulos)).catch(virhe => res.json({
- virhe: virhe.message
- }))
- }).delete((req, res) => {
- const mopoId = req.params.numero;
- mopot.poista(mopoId).then(tulos => res.json(tulos)).catch(virhe => res.json({
- virhe: virhe.message
- }))
- }).post((req, res) => {
- if (!req.body) res.json({
- virhe: 'ei löydy'
- });
- mopot.paivita(req.body).then(tulos => res.json(tulos)).catch(virhe => res.json({
- virhe: virhe.message
- }))
- }).put((req, res) => {
- if (!req.body) res.json({
- virhe: 'ei löydy'
- });
- mopot.lisaa(req.body).then(tulos => res.json(tulos)).catch(virhe => res.json({
- virhe: virhe.message
- }))
- });
- app.all('*', (req, res) => res.json('resurssia ei löydy tai yksilöivä numero puuttuu'));
- palvelin.listen(port, host, () => console.log(`Palvelin ${host} portissa ${port}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement