Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -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') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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;
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion models/productModel.js → app/models/productModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let products = require('../data/products')
const { v4: uuidv4 } = require('uuid')

const { writeDataToFile } = require('../utils')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

const { writeDataToFile } = require('../../utils')

function findAll() {
return new Promise((resolve, reject) => {
Expand Down
28 changes: 7 additions & 21 deletions server.js
Original file line number Diff line number Diff line change
@@ -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')


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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}`))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Server.listen(PORT, () => console.log(`Server running on port ${HOST}:${PORT}`))