diff --git a/app/app.js b/app/app.js new file mode 100644 index 0000000..ddfba1b --- /dev/null +++ b/app/app.js @@ -0,0 +1,23 @@ +const http = require('http') +const { getProducts, getProduct, createProduct, updateProduct, deleteProduct } = require('./controllers/productController') + +const Server = http.createServer((req, res) => { + if(req.url === '/api/products' && req.method === 'GET') { + getProducts(req, res) + } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'GET') { + const id = req.url.split('/')[3] + getProduct(req, res, id) + } else if(req.url === '/api/products' && req.method === 'POST') { + createProduct(req, res) + } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'PUT') { + const id = req.url.split('/')[3] + updateProduct(req, res, id) + } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'DELETE') { + const id = req.url.split('/')[3] + deleteProduct(req, res, id) + } else { + res.writeHead(404, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ message: 'Route Not Found' })) + } +}) +module.exports=Server; diff --git a/controllers/productController.js b/app/controllers/productController.js similarity index 98% rename from controllers/productController.js rename to app/controllers/productController.js index 68876cb..62dfa85 100644 --- a/controllers/productController.js +++ b/app/controllers/productController.js @@ -1,6 +1,6 @@ const Product = require('../models/productModel') -const { getPostData } = require('../utils') +const { getPostData } = require('../../utils') // @desc Gets All Products // @route GET /api/products diff --git a/data/products.json b/app/data/products.json similarity index 100% rename from data/products.json rename to app/data/products.json diff --git a/models/productModel.js b/app/models/productModel.js similarity index 95% rename from models/productModel.js rename to app/models/productModel.js index 8028d7e..f9d4db8 100644 --- a/models/productModel.js +++ b/app/models/productModel.js @@ -1,7 +1,7 @@ let products = require('../data/products') const { v4: uuidv4 } = require('uuid') -const { writeDataToFile } = require('../utils') +const { writeDataToFile } = require('../../utils') function findAll() { return new Promise((resolve, reject) => { diff --git a/server.js b/server.js index 524552a..0334834 100644 --- a/server.js +++ b/server.js @@ -1,26 +1,12 @@ const http = require('http') -const { getProducts, getProduct, createProduct, updateProduct, deleteProduct } = require('./controllers/productController') +const { getProducts, getProduct, createProduct, updateProduct, deleteProduct } = require('./app/controllers/productController') +const Server= require('./app/app') + -const server = http.createServer((req, res) => { - if(req.url === '/api/products' && req.method === 'GET') { - getProducts(req, res) - } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'GET') { - const id = req.url.split('/')[3] - getProduct(req, res, id) - } else if(req.url === '/api/products' && req.method === 'POST') { - createProduct(req, res) - } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'PUT') { - const id = req.url.split('/')[3] - updateProduct(req, res, id) - } else if(req.url.match(/\/api\/products\/([0-9]+)/) && req.method === 'DELETE') { - const id = req.url.split('/')[3] - deleteProduct(req, res, id) - } else { - res.writeHead(404, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify({ message: 'Route Not Found' })) - } -}) const PORT = process.env.PORT || 5000 +const HOST = process.env.HOST || 'http://localhost' + + -server.listen(PORT, () => console.log(`Server running on port ${PORT}`)) \ No newline at end of file +Server.listen(PORT, () => console.log(`Server running on port ${HOST}:${PORT}`)) \ No newline at end of file