-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (30 loc) · 1.06 KB
/
index.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
const http = require('http');
const utils = require('./utils');
const service = require('./service');
const { timeStamp, time } = require('console');
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(function (req, res) {
if (req.url == '/data' && req.method == 'GET') {
utils.readFile(function (err, data) {
if (err) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Error reading data');
console.log('Error:', err);
} else {
service.processData(data, function (result) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
});
}
});
} else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');
}
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});