美文网首页
Apache HTTP服务器

Apache HTTP服务器

作者: 似朝朝我心 | 来源:发表于2021-05-07 10:16 被阅读0次

Apache HTTP Server(简称Apache,音译为[阿帕奇])是一个开源的网页服务器,是一款相当流行的Web服务器软件。

Apache服务器软件默认有一个名叫www目录,所有存放在www目录中的资源都可以通过网址来浏览。

起步模板:

const http = require('http')
//1.创建Server服务器。
const server = http.createServer()
//2.监听Server的request请求事件,设置请求处理函数。
server.on('request', (req, res) => {
    //console.log(req.url)
    let url = req.url //保存用户输入的网址请求路径
    if (url === '/') {
        res.end('this is home page!')
    } else {
        res.end('404 Not Found')
    }
})
//3.绑定端口号,启动服务。
server.listen(8080, () => {
    console.log('Server is running at http://127.0.0.1:8080')
})

初步实现Apache功能,目录资源如下(index.js为入口):


读取文件操作(分别读取login.html页面、hello.txt、index.html页面):
const http = require('http')
const fs = require('fs')
const server = http.createServer()

server.on('request', (req, res) => {
  let url = req.url
  if (url === '/') {
    fs.readFile('C:/Users/asus/Desktop/前端学习/其余/Node/www/index.html', (err, data) => {
      if (err) {
        return res.end('404 Not Found')//return有2个作用:1.方法返回值,2.阻止代码继续往后执行(这里用了这个作用)
      }
      res.end(data)
    })
  } else if (url === '/hello.txt') {
    fs.readFile('C:/Users/asus/Desktop/前端学习/其余/Node/www/hello.txt', (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  } else if (url === '/index.html') {
    fs.readFile('C:/Users/asus/Desktop/前端学习/其余/Node/www/index.html', (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  } else if (url === '/user/login.html') {
    fs.readFile('C:/Users/asus/Desktop/前端学习/其余/Node/www/user/login.html', (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  }
})

server.listen(8080, () => {
  console.log('Server is running at http://127.0.0.1:8080')
})

当用户发生网址路径请求时,跳转到对应的页面效果:


路径抽离优化思想:

显然我们每次读取文件的路径前半段是一模一样的,我们是否可以用一个变量保存着?

const wwwDir = 'C:/Users/asus/Desktop/前端学习/其余/Node/www/'
fs.readFile(`${wwwDir}index.html`, () => {

})

完整代码:

const http = require('http')
const fs = require('fs')
const server = http.createServer()

const wwwDir = 'C:/Users/asus/Desktop/前端学习/其余/Node/www/'

server.on('request', (req, res) => {
  let url = req.url
  if (url === '/') {
    fs.readFile(`${wwwDir}index.html`, (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  } else if (url === '/hello.txt') {
    fs.readFile(`${wwwDir}hello.txt`, (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  } else if (url === '/index.html') {
    fs.readFile(`${wwwDir}index.html`, (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  } else if (url === '/user/login.html') {
    fs.readFile(`${wwwDir}user/login.html`, (err, data) => {
      if (err) {
        return res.end('404 Not Found')
      }
      res.end(data)
    })
  }
})

server.listen(8080, () => {
  console.log('Server is running at http://127.0.0.1:8080')
})

动态路径读取:
首先我们新增如下资源(用于测试):

代码:

const http = require('http')
const fs = require('fs')
const server = http.createServer()

const wwwDir = 'C:/Users/asus/Desktop/前端学习/其余/Node/www'

server.on('request', (req, res) => {
    let url = req.url
    let filePath = '/index.html'
    if (url !== '/') { //如果不是请求首页
        filePath = url //那么就把用户在网址上更改后的路径保存到filePath,这时候的filePath 也做出响应的变化。
    }

    fs.readFile(`${wwwDir}${filePath}`, (err, data) => {
        if (err) {
            return res.end('404 Not Found')
        }
        res.end(data)
    })
})

server.listen(8080, () => {
    console.log('Server is running at http://127.0.0.1:8080')
})

效果:


绑定动态路径的好处:只要在www目录下存在的资源,用户皆可以通过寻址匹配到对应的页面视图。

相关文章

网友评论

      本文标题:Apache HTTP服务器

      本文链接:https://www.haomeiwen.com/subject/xgrybktx.html