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:any, res:any) => { res.send({ express: 'Hello From Express' }); }); app.post('/api/world', (req:any, res:any) => { 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:any, res:any) => { console.log('Create song: ' + JSON.stringify(req.body)) let include:any[] = []; 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:any, res:any) => { console.log("List songs"); models.Song.findAll({ include: [models.Artist] }) .then((songs:any[]) => { res.send(songs); }); }); app.post('/artist/create', (req:any, res:any) => { console.log('Create artist: ' + req.body.name) const artist = models.Artist.build(req.body); artist.save(); res.sendStatus(200); }); app.get('/artist/list', (req:any, res:any) => { console.log("List artists"); models.Artist.findAll() .then((artists:any[]) => { res.send(artists); }); }); models.sequelize.sync().then(() => { app.listen(port, () => console.log(`Listening on port ${port}`)); }) export {}