-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (29 loc) · 845 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 http = require('http')
let data = {
"pattern": [
{ "id": 1, "type": "tablet", "name": "Samsung" },
{ "id": 2, "type": "clock", "name": "Apple" },
{ "id": 3, "type": "smartphone", "name": "Philips" },
{ "id": 4, "type": "clock", "name": "Realme" }
]
}
const server = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, {
'Content-Type': 'application/json'
})
res.end(JSON.stringify(data))
console.log(req.url)
} else {
res.writeHead(300, {
'Content-Type': 'text/plain'
})
res.end('Error! Uncorrect request!')
console.log(req.url)
}
})
const port = 3000
server.listen(port, (err) => {
if (err) console.error(err)
console.log(`Server was started on localhost:${port}`)
})