创建线上文件服务
const http = require('http')
//创建web服务器
const server = http.createServer()
//监听web服务器request事件
server.on('request', (req, res) => {
const url = req.url
//把请求的url地址,映射为本地文件的存放路径
// const fpath = path.join(__dirname, url)
let fpath = ''
if (url == '/') {
fpath = path.join(__dirname, './clock/index1.html')
} else {
fpath = path.join(__dirname, './clock', url)
}
fs.readFile(fpath, 'utf-8', function(err, data) {
if (err) return res.end('404 not found')
res.end(data)
})
})
//启动服务器
server.listen(80, () => {
console.log('server running at http://127.0.0.1')
})