Typescript爬虫实战(1) ---- 用express搭建服务端

  • 安装express:
    npm install express -S
    在新版本的express中,安装express同时也会安装其相应的类型文件

开始构建逻辑

初始化爬虫接口
  • 搭建http服务:
//Request,Response从express中引用
import express,{Response,Request} from 'express'
import router from './router'
const app = express()

app.use(router)

app.listen(7001,()=>{
  console.log('server running on 7001')
})
  • 搭建router层,编写接口。
import {Router,Request,Response} from 'express'
import Crowller from './crowller'
import WebAnalyzer from './analyzer'

const router = Router()
  router.get('/',(req:Request,res:Response)=>{
  res.send('hello express in ts')
})

router.get('/getData',(req:Request,res:Response)=>{
  const secret = 'x3b174jsx'
  const url = `http://www.dell-lee.com/typescript/demo.html?secret=${secret}`
  const analyzer = new WebAnalyzer()
  new Crowller(url,analyzer)
  res.send('success')
})


export default router;

你可能感兴趣的:(Typescript爬虫实战(1) ---- 用express搭建服务端)