-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (26 loc) · 894 Bytes
/
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
const express = require('express');
const app = express();
const http = require('http');
const url = require('url');
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url, true);
if (reqUrl.pathname === '/gasto-viagem' && req.method === 'GET') {
const { distancia, preco, consumo } = reqUrl.query;
const distance = parseFloat(distancia);
const fuelPrice = parseFloat(preco);
const fuelConsumption = parseFloat(consumo);
const totalCost = (distance / fuelConsumption) * fuelPrice;
const response = {
custoTotal: totalCost.toFixed(2)
};
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(response));
} else {
res.statusCode = 404;
res.end('Endpoint não encontrado');
}
});
const port = 8080;
server.listen(port, () => {
console.log(`Servidor iniciado na porta ${port}`);
});