zoukankan      html  css  js  c++  java
  • node--静态服务器

    1、同步读取文件

    const data = fs.readFileSync('./model/mime.json');
     
    // 这里是添加了可以正常链接其他格式文件的服务器
    const http = require('http')
    const url = require('url')
    const fs = require('fs')
    const path = require('path')
    const mimeModel = require('./model/getmimefromfile')
    
    http.createServer((req, res) => {
      // 只取路径,不要后面的查询字符串
      let pathName = url.parse(req.url).pathname
    
      // 过滤无效请求
      if (pathName === '/') {
        //默认加载首页
        pathName = '/index.html'
      }
    
      //获取文件后缀名
      const extname = path.extname(pathName)
    
      if (pathName !== '/favicon.ico') {
        // 文件操作引入static下的相应文件
    
        //静态托管文件
        fs.readFile('static/' + pathName, (err, data) => {
          if (err) {
            // 错误处理
            fs.readFile('static/404.html', (err, data) => {
              res.writeHead(200, {
                'Content-Type': "text/html;charset=utf8"
              })
              res.write(data)
              res.end()
            })
          } else {
            // 记得设置相应头
            let mime = mimeModel.getMime(fs, extname)
            console.log(mime)
            res.writeHead(200, {
              "Content-Type": mime + ";charset='utf8'"
            })
            res.write(data)
            res.end()
          }
        })
      }
    }).listen(8080, () => {
      console.log('server is running')
    })
  • 相关阅读:
    PHP和Ajax设置页面请求超时
    Flex 布局教程
    数据库访问优化法则
    phpcms网站搬家至服务器
    phpcms网页替换验证码及搜索功能
    php判断手机段登录
    php环境搭建
    ThinkPHP框架
    JQuery事件
    JQuery
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/11143929.html
Copyright © 2011-2022 走看看