-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
27 lines (24 loc) · 865 Bytes
/
Copy pathapp.js
File metadata and controls
27 lines (24 loc) · 865 Bytes
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
const tweets = [
{ id: 1, text: 'Hello World' },
{ id: 2, text: 'Hello Universe' },
{ id: 3, text: 'Hello Galaxy' },
];
function doThisOnIncomingData(incomingData, functionToSetOutgoingData) {
const tweet = tweets[incomingData.url.slice(incomingData.url.length-1)];
if (!tweet) throw new Error('Tweet not found');
functionToSetOutgoingData.end(JSON.stringify(tweet));
}
const http = require('http'); // require is a function that is used to import a module
const server = http.createServer();
server.listen(3000);
server.on('request', (req, res) => {
try {
doThisOnIncomingData(req, res);
} catch (error) {
res.statusCode = 500;
res.end(`${error} | Something went wrong`);
}
});
server.on('clientError', (err, socket) => {
socket.end(`${error} HTTP/1.1 400 Bad Request\r\n\r\n`);
});