-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (116 loc) · 3.43 KB
/
server.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const express = require('express')
const metrics = require('./metrics');
const Product = require('./models/productModel')
const app = express()
const mongoose = require('mongoose');
app.use(express.json())
app.use(express.urlencoded({extended: false}))
// Configuration
var config = require('./configure');
conn_string = config.mongo_db.connection_string
console.log("Connecting to %s", conn_string)
mongoose.set("strictQuery", false)
STATE = config.state;
// Middleware to measure HTTP request duration
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const end = Date.now();
const duration = (end - start) / 1000; // Convert to seconds
metrics.httpRequestDuration.labels(req.method, req.path).observe(duration);
});
next();
});
//routes
app.get('/', (req, res) => {
res.send('Hello NODE API')
})
// Expose metrics endpoint
app.get('/metrics', (req, res) => {
res.set('Content-Type', metrics.register.contentType);
metrics.register.metrics().then((metricsData) => {
res.send(metricsData);
}).catch((err) => {
console.error(err);
res.status(500).end();
});
});
app.get('/blog', (req, res) => {
if (STATE == "DEVELOPMENT") {
res.send('Hello We are in Development state.');
}
else
{
res.send('Hello We are in Production state.');
}
})
app.get('/products', async(req, res) => {
try {
const products = await Product.find({});
res.status(200).json(products);
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.get('/products/:id', async(req, res) =>{
try {
const {id} = req.params;
const product = await Product.findById(id);
res.status(200).json(product);
} catch (error) {
res.status(500).json({message: error.message})
}
})
app.post('/products', async(req, res) => {
try {
const product = await Product.create(req.body)
res.status(200).json(product);
} catch (error) {
console.log(error.message);
res.status(500).json({message: error.message})
}
})
// update a product
app.put('/products/:id', async(req, res) => {
try {
const {id} = req.params;
const product = await Product.findByIdAndUpdate(id, req.body);
// we cannot find any product in database
if(!product){
return res.status(404).json({message: `cannot find any product with ID ${id}`})
}
const updatedProduct = await Product.findById(id);
res.status(200).json(updatedProduct);
} catch (error) {
res.status(500).json({message: error.message})
}
})
// delete a product
app.delete('/products/:id', async(req, res) =>{
try {
const {id} = req.params;
const product = await Product.findByIdAndDelete(id);
if(!product){
return res.status(404).json({message: `cannot find any product with ID ${id}`})
}
res.status(200).json(product);
} catch (error) {
res.status(500).json({message: error.message})
}
})
async function start() {
try{
await mongoose.connect(conn_string);
console.log("Now starting the server...");
}
catch (error) {
console.error(error);
return null;
}
}
start().then(
app.listen(3000, ()=> {
console.log(`Node API app is running on port 3000`)
})
)
module.exports = app;