const express = require("express"); const cors = require("cors"); const bcrypt = require("bcryptjs"); const app = express(); const corsOptions = { origin: "http://localhost:8080" }; app.use(cors(corsOptions)); // parse requests of content-type - application/json app.use(express.json()); // parse requests of content-type - application/x-www-form-urlencoded app.use(express.urlencoded({ extended: true })); // database const db = require("./models"); const Role = db.role; const User = db.user; const PitchType = db.pitchType; // db.sequelize.sync(); // force: true will drop the table if it already exists db.sequelize.sync({force: true}).then(() => { console.log('Drop and Re-sync Database with { force: true }'); initial(); }); // simple route app.get("/", (req, res) => { res.json({ message: "Welcome to bullpen application." }); }); // routes require('./routes/auth.routes')(app); require('./routes/user.routes')(app); require('./routes/pitchType.routes')(app); // set port, listen for requests const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}.`); }); function initial() { Role.bulkCreate([ { name: 'user' }, { name: 'moderator' }, { name: 'administrato' }, ]); User.bulkCreate([ { firstName: 'Nolan', lastName: 'Ryan', dateOfBirth: new Date(1947, 1, 31), email: 'ryan.nolan@bullpen.com', password: bcrypt.hashSync('nolan', 8) }, { firstName: 'Sandy', lastName: 'Koufax', dateOfBirth: new Date(1935, 12, 30), email: 'sandy.koufax@bullpen.com', password: bcrypt.hashSync('sandy', 8) }, { firstName: 'Pedro', lastName: 'Martinez', dateOfBirth: new Date(1971, 10, 25), email: 'pedro.martinez@bullpen.com', password: bcrypt.hashSync('pedro', 8) }, { firstName: 'randy', lastName: 'johnson', dateOfBirth: new Date(1963, 9, 10), email: 'randy.johnson@bullpen.com', password: bcrypt.hashSync('randy', 8) } ]); PitchType.bulkCreate([ { name: 'Fastball', abbreviation: 'FB' }, { name: 'Curveball', abbreviation: 'CB' }, { name: 'Slider', abbreviation: 'SL' }, { name: 'Changeup', abbreviation: 'CH' }, { name: 'Cutter', abbreviation: 'CUT' }, { name: 'Sweeper', abbreviation: 'SW' }, { name: 'Slurve', abbreviation: 'SLV' }, ]); }