NodeJs学习日报——day3


// 导入模块
const http = require('http')

// 创建web服务器实例
const server = http.createServer()

// 为服务器实例绑定request事件,监听客户端请求
server.on('request', function (req, resp) {
    console.log("Someone visit our web server.");
})

// 启动服务器
server.listen(8080, function () {
    console.log("server running at http://120.0.0.1:8080");
})
// 导入模块
const http = require('http')

// 创建web服务器实例
const server = http.createServer()

server.on('request', (req) => {
    const url = req.url
    const method = req.method
    const str = `Your request url is ${url},and request method is ${method}`
    console.log(str);
})

// 启动服务器
server.listen(8080, function () {
    console.log("server running at http://120.0.0.1:8080");
})
// 导入模块
const http = require('http')

// 创建web服务器实例
const server = http.createServer()

server.on('request', (req, resp) => {
    // 设置编码格式
    resp.setHeader('Content-Type', 'text/html; charset=utf-8')

    const url = req.url
    const method = req.method
    const str = `你请求的url是 ${url},请求方法是 ${method}`
    console.log(str);

    // 调用resp.end方法,响应信息
    resp.end(str)
})

// 启动服务器
server.listen(8080, function () {
    console.log("server running at http://120.0.0.1:8080");
})
const fs = require('fs')
const path = require('path')

// 定义正则表达式,分别匹配style和script
const regStyle = /', '')

    // 将提取的样式,存储到index.css文件中
    fs.write(path.join(__dirname, "./clock/index.css"), newCss, function(err) {
        if (err) {
            return console.log("css文件写入失败" + err.message);
        }
        console.log("css文件写入成功");
    })
}

// 定义处理js样式的方法
function resolveJs(JsStr) {
    // 使用正则提取需要的内容
    const r2 = regScript.exec(JsStr)

    // 将提取出来的样式字符串,进行字符串的replace替换操作
    const newJs = r2[0].replace('', '')

    // 将提取的样式,存储到index.css文件中
    fs.write(path.join(__dirname, "./clock/index.js"), newJs, function(err) {
        if (err) {
            return console.log("js文件写入失败" + err.message);
        }
        console.log("js文件写入成功");
    })
}

// 定义处理html文件的方法
function resolveHtml(htmlStr) {
    // 将字符串调用replace方法,把内嵌的style和script替换成内联标签
    const newHtml = htmlStr.replace(regStyle, '')
    .replace(regScript, '')

    // 写入html文件
    fs.write(path.join(__dirname, './clock/index.html'), newHtml, function(err) {
        if (err) {
            return console.log("html文件写入失败" + err.message);
        }

        console.log("文件写入成功");
    })
}