-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (35 loc) · 1.41 KB
/
Copy pathserver.js
File metadata and controls
47 lines (35 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require('dotenv').config(); //Calling the environment variables
const PORT = process.env.PORT || 3000;
const express = require('express'); //Calling the express function from node_modules
const app = express();
const {
getProducts,
addProduct,
patchProduct,
deleteProduct,
getProduct
} = require('./Controllers/Routes_handlers');//
app.use(express.json()); //In-built express middleware for json parsing
// Root route (homepage) of the API
app.get('/', (req, res) => {
res.send('Product Inventory API is running');
});// Sends a simple text response to confirm the API is running
// Route to get all products
app.get('/products', getProducts);// assign GET route to the imported function
// Route to add new products
app.post('/products', addProduct);// assign POST route to the imported function
// Route to edit existing product details
app.patch('/products/:id', patchProduct);// assign PATCH route to the imported function
// Route to delete a product by id
app.delete('/products/:id', deleteProduct);// assign DELETE route to the imported function
// Route to get a product by id
app.get('/products/:id', getProduct);// assign GET route to the imported function
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
});
app.use((err, req, res, next) => {
res.status(500).json({error: 'Server error!'});
});
app.use((req, res,) => {
res.status(404).json({message: "Invalid route"});
});