NodeJS学习日报day4——模块化


// console.log(module);

// 执行顺序不同,结果也不同
// module.exports = {
//     name : 'Cra2iTeT',
//     hi() {
//         console.log('hi');
//     }
// }

// 挂在userName属性
module.exports.userName = '张三'

module.exports.hello = function() {
    console.log('hello');
}

module.exports = {
    name : 'Cra2iTeT',
    hi() {
        console.log('hi');
    }
}
const http = require('http')
const fs = require('fs')
const path = require('path')

const server = http.createServer()

server.on('request', (req, resp) => {
    const url = req.url

    const fPath = path.join(__dirname, '../day2', url)
    console.log(fPath);

    fs.readFile(fPath, 'utf-8', function(err, dataStr) {
        if (err) {
            console.log("404 not found" + err.message);
        }
        
        // 加上响应头以后css样式无法显示
        // resp.setHeader('Content-Type', 'text/html; charset=utf-8')
        resp.end(dataStr)
    })
})

// server.on('request', (req, resp) => {
//     const url = req.url

//     let fPath = ''
//     if (url === '/' || url === '/clock/index.html' || url === '') {
//         fPath = path.join(__dirname, '../day2', '/clock/index.html')
//     } else {
//         fPath = path.join(__dirname, '../day2', url)
//     }

//     console.log(fPath);

//     fs.readFile(fPath, 'utf-8', function(err, dataStr) {
//         if (err) {
//             console.log("404 not found " + err.message);
//         }
//         // 加上响应头以后css样式无法显示
//         // resp.setHeader('Content-Type', 'text/html; charset=utf-8')
//         resp.end(dataStr)
//     })
// })

server.listen(8080, () => {
    console.log('server running at http://127.0.0.1:8080');
})
const http = require('http')
const server = http.createServer()

server.on('request', (req, resp) => {
    const url = req.url

    let content = '

404 not found

' if (url === '/' || url === '/index.html') { content = '

首页

' } else if (url === '/about.html') { content = '

关于页面

' } resp.setHeader('Content-Type', 'text/html; charset=utf-8') resp.end(content) }) server.listen(8080, () => { console.log('server running at http://127.0.0.1:8080'); })