const http = require('http')
const url = require('url')
const fs = require('fs')
const path = require('path')
const contType = require('./contType'Http.createserver ((req, res) => {// Create a serviceletParse (req.url). Pathname // Obtain the current route pathname ==='/'? pathname = '/index.html':pathname // Loads the home page by defaultletExtname = path. extName (pathname) // Obtain the route extensionif(req.url ! = ='/favicon.ico') {// Filter request fs.readfile ('pages/' + pathname, (err, data) => {
      if(err) {// Check whether the page link is valid, if not, display 404 page fs.readfile ('pages/404.html', (errs, datas) => { errs? Console. log(errs): errs // Check whether the 404 page has res.writehead (404, {'Content-Type': 'text/html; charset="utf-8"'})
          res.write(datas)
          res.end()
        })
      } else {
        letConttype.typelist (extname) console.log(ct) res.writehead (200, {conttype.typelist (extname))'Content-Type': ' '+ ct +' ;charset="utf-8"'Res}) res. Write (data). The end ()}})}}). Listen (9999) / / add to monitor windowCopy the code

contType.js

const contType = {
  typelist: t => {
    switch (t) {
      case '.html':
        return 'text/html'
      case '.css':
        return 'text/css'
      case '.js':
        return 'text/javascript'
      default:
        return 'text/html'
    }
  }
}
module.exports = contType
Copy the code