SUBD/node-postgres/index.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

const express = require('express')
const cors = require('cors')
const faculty = require('./models/faculty')
const app = express()
const port = 3001
app.use(cors())
app.use(express.json())
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000')
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers')
next();
});
// ФАКУЛЬТЕТ-------------------------------------
app.post('/faculty', (req, res) => {
faculty.create(req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.get('/', (req, res) => {
faculty.get()
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.delete('/faculty/del/:id', (req, res) => {
faculty.del(req.params.id)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
app.put('/faculty/upd/:id', (req, res) => {
faculty.update(req.params.id, req.body.name)
.then(response => res.status(200).send(response))
.catch(error => res.status(500).send(error))
})
//----------------------------------------------
//
// app.post('/direction', (req, res) => {
// faculty.createFaculty(req.body.name)
// .then(response => res.status(200).send(response))
// .catch(error => res.status(500).send(error))
// })
// app.get('/', (req, res) => {
// faculty.getFaculties()
// .then(response => res.status(200).send(response))
// .catch(error => res.status(500).send(error))
// })
app.listen(port, () => {
console.log(`app runnong on port ${port}`)
})