64 lines
1.2 KiB
JavaScript
64 lines
1.2 KiB
JavaScript
|
|
const http = require('http')
|
|
const fs = require('fs')
|
|
const url = require('url')
|
|
|
|
const sendMyFile = (file, res) => {
|
|
fs.readFile(file, (err, data) => {
|
|
if (!err) {
|
|
res.writeHead(200, {
|
|
'content-type': 'text/html; charset=utf-8'
|
|
})
|
|
res.end(data)
|
|
}
|
|
else
|
|
{
|
|
res.writeHead(404)
|
|
res.end('file not found')
|
|
}
|
|
})
|
|
}
|
|
|
|
//const server = http.createServer((req, res) => {
|
|
//
|
|
// let pathName = url.parse(req.url).pathname
|
|
//
|
|
// if (req.method === "GET") {
|
|
// if (pathName === "/")
|
|
// sendMyFile(__dirname + "/index.html", res)
|
|
// else if (pathName === "/index.html")
|
|
// sendMyFile(__dirname + "/index.html", res)
|
|
// else
|
|
// {
|
|
// res.writeHead(404)
|
|
// res.end('wrong url')
|
|
// }
|
|
// }
|
|
//})
|
|
|
|
|
|
let myExpress = () => {
|
|
return http.createServer((req, res) => {
|
|
let pathName = url.parse(req.url).pathname
|
|
if (req.method === "GET") {
|
|
if (pathName === "/")
|
|
sendMyFile(__dirname + "/index.html", res)
|
|
else
|
|
{
|
|
res.writeHead(404)
|
|
res.end('wrong url')
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
//server.get('/', (req, res) => {
|
|
// res.sendFile(__dirname + "/" + "index.html")
|
|
//});
|
|
|
|
const server = myExpress();
|
|
|
|
const port = 3001;
|
|
server.listen(port, console.log(`listening on port ${port}`));
|
|
|