Node.js服务端04-自制路由

61 阅读1分钟

自制简易路由

const http = require('http')
const fs = require('fs')

http.createServer((req, res) => {

  const myURL = new URL(req.url, "http://127.0.0.1")
  // console.log(myURL.pathname);
  switch (myURL.pathname) {
    case '/login':
      res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
      res.write(fs.readFileSync('./static/login.html'), 'utf-8')
      break
    case '/home':
      res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
      res.write(fs.readFileSync('./static/home.html'), 'utf-8')
      break
    default:
      res.writeHead(404, {'Content-Type':'text/html;charset=utf-8'})
      res.write(fs.readFileSync('./static/404.html'), 'utf-8')
      break
  }
  res.end()
}).listen(3000, ()=> {
  console.log('server start');
})

初步改进

const fs = require('fs')

function render(res, path) {
  res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
  res.write(fs.readFileSync(path), 'utf-8')
  res.end()
}

const route = {
  '/login': (res) => {
    render(res, './static/login.html')
  },
  '/home': (res) => {
    render(res, './static/home.html')
  },
  '/404': (res) => {
    res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
    res.write(fs.readFileSync('./static/404.html'), 'utf-8')
    res.end()
  },
}

module.exports = route
const http = require('http')
const fs = require('fs')

const route = require('./route')

http.createServer((req, res) => {

  const myURL = new URL(req.url, "http://127.0.0.1")
  // console.log(myURL.pathname);
  try {
    route[myURL.pathname](res)
  } catch (error) {
    route['/404'](res)
  }
  
}).listen(3000, ()=> {
  console.log('server start');
})

调整入口

const server = require('./server')

server.start()
const http = require('http')

const route = require('./route')

function start() {

  http.createServer((req, res) => {

    const myURL = new URL(req.url, "http://127.0.0.1")
    // console.log(myURL.pathname);
    try {
      route[myURL.pathname](res)
    } catch (error) {
      route['/404'](res)
    }

  }).listen(3000, () => {
    console.log('server start');
  })
}

路由兼容API

function render(res, data, type='') {
  res.writeHead(200, {'Content-Type':`${type?type:'application/json'};charset=utf8`})
  res.write(data)
  res.end()
}

const apiRouter = {
  '/api/login':(res) => {
    render(res, `{ok:1}`)
  }
}

module.exports = apiRouter
const fs = require('fs')

function render(res, path) {
  res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
  res.write(fs.readFileSync(path), 'utf-8')
  res.end()
}

const route = {
  '/login': (res) => {
    render(res, './static/login.html')
  },
  '/home': (res) => {
    render(res, './static/home.html')
  },
  '/404': (res) => {
    res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'})
    res.write(fs.readFileSync('./static/404.html'), 'utf-8')
    res.end()
  },
}
const http = require('http')

const route = require('./route')

const api = require('./api')

const Router = {}

Object.assign(Router, route)
Object.assign(Router, api)

function start() {

  http.createServer((req, res) => {

    const myURL = new URL(req.url, "http://127.0.0.1")
    // console.log(myURL.pathname);
    try {
      Router[myURL.pathname](res)
    } catch (error) {
      Router['/404'](res)
    }

  }).listen(3000, () => {
    console.log('server start');
  })
}

exports.start = start