Skip to content

Commit ae67788

Browse files
committed
chapter 04: create a basic webservice in order to create an example that I want to create from the book
1 parent a74aebb commit ae67788

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

loggerWebService/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const server = require('./server');
2+
const port = process.env.PORT || 3000;
3+
4+
server.listen(port, function startServer() {
5+
console.log(
6+
`Webservice is ready to start listening requests on http://localhost:${port}`
7+
);
8+
});

loggerWebService/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const http = require('http');
2+
3+
const server = http.createServer(function getRequest(request, response) {
4+
if (request.method !== 'POST') {
5+
response.statusCode = 405;
6+
return response.end();
7+
}
8+
9+
request.setEncoding('utf8');
10+
11+
let data = '';
12+
13+
request.on('data', function getData(chunk) {
14+
data += chunk;
15+
});
16+
17+
request.on('end', function getServerResponse() {
18+
console.log(
19+
`Writing the error in the stdout, waiting that an hipotetic tool like influxdb read the info and save it:
20+
${data}
21+
`
22+
);
23+
response.statusCode = 201;
24+
response.end();
25+
});
26+
});
27+
28+
module.exports = server;

0 commit comments

Comments
 (0)