-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
27 lines (25 loc) · 750 Bytes
/
server.js
File metadata and controls
27 lines (25 loc) · 750 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 http = require('http')
const path = require('path')
const fs = require('fs')
const port = 3030
const server = http.createServer()
server.on('request', (req, res) => {
if (req.url === '/' || req.url === '/index.html') {
const filePath = path.join(__dirname, 'index.html')
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('404 Not Found')
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(data)
}
})
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('404 Not Found')
}
})
server.listen({ port }, () => {
console.log(`Server running at http://localhost:${port}/`)
})