From 518840a5764a688e7241a255e1d44ec3082a2427 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Fri, 4 Jan 2019 16:51:27 -0800 Subject: [PATCH] setting up route with proper mini-apps with express.Router() and app.use() --- app/models/donors.model.js | 2 +- app/routes/donors.route.js | 38 ++++++++++++++++++++------------------ server.js | 9 +++++---- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/models/donors.model.js b/app/models/donors.model.js index adebd74..58d106a 100644 --- a/app/models/donors.model.js +++ b/app/models/donors.model.js @@ -10,7 +10,7 @@ module.exports = (sequelize, DataTypes) => { last_name: DataTypes.STRING, email: DataTypes.STRING, age: DataTypes.INTEGER, - phone: DataTypes.INTEGER, + phone: DataTypes.BIGINT, address: DataTypes.STRING, city: DataTypes.STRING, state: DataTypes.STRING, diff --git a/app/routes/donors.route.js b/app/routes/donors.route.js index b39734c..371e9d9 100644 --- a/app/routes/donors.route.js +++ b/app/routes/donors.route.js @@ -1,19 +1,21 @@ -module.exports = function(app) { - - const donors = require('../controllers/donors.controller.js'); - - // Create a new Donor - app.post('/api/donors', donors.create); - - // Retrieve all Donor - app.get('/api/donors', donors.findAll); - - // Retrieve a single Donor by Id - app.get('/api/donors/:DonorId', donors.findById); +const express = require('express'), + router = express.Router(); - // Delete a Donor with Id - app.delete('/api/donors/:DonorId', donors.delete); - - // // Update a Donor with Id - // app.put('/api/donors/:DonorId', donors.update); -} \ No newline at end of file +const donors = require('../controllers/donors.controller.js'); + +// Create a new Donor +router.post('/', donors.create); + +// Retrieve all Donor +router.get('/', donors.findAll); + +// Retrieve a single Donor by Id +router.get('/:DonorId', donors.findById); + +// Delete a Donor with Id +router.delete('/:DonorId', donors.delete); + +// // Update a Donor with Id +// router.put('/api/donors/:DonorId', donors.update); + +module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index 4122275..fafd2cd 100644 --- a/server.js +++ b/server.js @@ -19,18 +19,19 @@ db.sequelize }); // force: true will drop the table if it already exists -db.sequelize.sync({force: true}).then(() => { - console.log('Drop and Resync with { force: true }'); +const drop_tables = true; +db.sequelize.sync({force: drop_tables}).then(() => { + console.log(`Drop and Resync with { force: ${drop_tables} }`); }); //define a route, usually this would be a bunch of routes imported from another file -router.get('/', function (req, res, next) { +app.get('/', function (req, res, next) { res.send('Welcome to the Ucharify API'); }); //adding routes to Express app -require('./app/routes/donors.route.js')(app); +app.use('/api/donors', require('./app/routes/donors.route.js')); // Create a Server var server = app.listen(port, function () {