zoukankan      html  css  js  c++  java
  • http-helloworld

    var http = require('http')
    var fs = require('fs')

    // 1. 创建 Server
    var server = http.createServer()

    // 2. 监听 Server 的 request 请求事件,设置请求处理函数
    // 请求
    // 处理
    // 响应
    // 一个请求对应一个响应,如果在一个请求的过程中,已经结束响应了,则不能重复发送响应。
    // 没有请求就没有响应。
    //
    // 咱们以前使用过 Apache 服务器软件,这个软件默认有一个 www 目录,所有存放在 www 目录中的资源都可以通过网址来浏览
    // 127.0.0.1:80/a.txt
    // 127.0.0.1:80/index.html
    // 127.0.0.1:80/apple/login.html

    var wwwDir = 'D:/Movie/www'

    server.on('request', function (req, res) {
    var url = req.url
    // / index.html
    // /a.txt wwwDir + /a.txt
    // /apple/login.html wwwDir + /apple/login.html
    // /img/ab1.jpg wwwDir + /img/ab1.jpg
    if (url === '/') {
    fs.readFile(wwwDir + '/index.html', function (err, data) {
    // if (err) {
    // res.end('404 Not Found.')
    // } else {

    // }

    if (err) {
    // return 有两个作用:
    // 1. 方法返回值
    // 2. 阻止代码继续往后执行
    return res.end('404 Not Found.')
    }
    res.end(data)
    })
    } else if (url === '/a.txt') {
    fs.readFile(wwwDir + '/a.txt', function (err, data) {
    if (err) {
    return res.end('404 Not Found.')
    }
    res.end(data)
    })
    } else if (url === '/index.html') {
    fs.readFile(wwwDir + '/index.html', function (err, data) {
    if (err) {
    return res.end('404 Not Found.')
    }
    res.end(data)
    })
    } else if (url === '/apple/login.html') {
    fs.readFile(wwwDir + '/apple/login.html', function (err, data) {
    if (err) {
    return res.end('404 Not Found.')
    }
    res.end(data)
    })
    }
    })

    // 3. 绑定端口号,启动服务
    server.listen(3000, function () {
    console.log('running...')
    })
  • 相关阅读:
    个人所得税避税的10种方法
    营业税
    融资租赁
    会计等式
    公司公积金
    fixed语句
    自由之路
    $or操作符
    $in 操作符
    Redis 字典的实现
  • 原文地址:https://www.cnblogs.com/lujieting/p/10296317.html
Copyright © 2011-2022 走看看