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.
62 lines
1.4 KiB
62 lines
1.4 KiB
const express = require('express'); |
|
const bodyParser = require('body-parser'); |
|
const models = require('./models'); |
|
|
|
const app = express(); |
|
|
|
// TODO: configurable port |
|
const port = process.env.PORT || 5000; |
|
|
|
app.use(bodyParser.json()); |
|
app.use(bodyParser.urlencoded({ extended: true })); |
|
|
|
app.get('/api/hello', (req, res) => { |
|
res.send({ express: 'Hello From Express' }); |
|
}); |
|
|
|
app.post('/api/world', (req, res) => { |
|
console.log(req.body); |
|
res.send( |
|
`I received your POST request. This is what you sent me: ${req.body.post}`, |
|
); |
|
}); |
|
|
|
app.post('/song/create', (req, res) => { |
|
console.log('Create song: ' + JSON.stringify(req.body)) |
|
include = []; |
|
if ("Artist" in req.body) { |
|
include.push(models.Artist); |
|
} |
|
const song = models.Song.build(req.body, { include: include }); |
|
song.save(); |
|
res.sendStatus(200); |
|
}); |
|
|
|
app.get('/song/list', (req, res) => { |
|
console.log("List songs"); |
|
models.Song.findAll({ |
|
include: [models.Artist] |
|
}) |
|
.then(songs => { |
|
res.send(songs); |
|
}); |
|
}); |
|
|
|
app.post('/artist/create', (req, res) => { |
|
console.log('Create artist: ' + req.body.name) |
|
const artist = models.Artist.build(req.body); |
|
artist.save(); |
|
res.sendStatus(200); |
|
}); |
|
|
|
app.get('/artist/list', (req, res) => { |
|
console.log("List artists"); |
|
models.Artist.findAll() |
|
.then(artists => { |
|
res.send(artists); |
|
}); |
|
}); |
|
|
|
models.sequelize.sync().then(() => { |
|
app.listen(port, () => console.log(`Listening on port ${port}`)); |
|
}) |