Giter Site home page Giter Site logo

nodejs-crawler's Introduction

nodejs-crawler

使用koa2 + mongodb + redis 爬取segmentfault头条首页数据中的最新和最热数据

如何运行

首先需要确保已安装 mongodb和redis 并以开启mongodb和redis服务,之后执行以下命令

cd nodejs-crawler
npm install
npm start

打开浏览器访问localhost:3000/users/news 或者 localhost:3000/users/newest

总体结构

  • routes 处理请求
  • lib/controllers: 确保有数据返回(无数据就去执行爬虫) 主要是业务逻辑操作
  • lib/bll: 完全对数据库操作 返回Promise
  • lib/model: 数据库映射
  • lib/crawler: 爬虫程序,返回Promise

流程

  • 客户端发送请求

  • 服务端接收请求
    2.1 redis缓存中有无数据.有数据则返回;无数据则查询mongodb
    2.2 查询mongodb有数据则缓存到redis并返回,无数据则去执行爬虫
    2.3 爬虫获取的结果保存到mongodb中

环境安装

安装mongodb

brew install mongodb

指定数据存储目录:

mkdir -p /data/db
mongod --dbpath=/data/db --port=27017 #启动mongodb服务
mongod --dbpath=/data/db --port=27017 --fork #以守护进程方式启动

安装redis

brew install redis

启动redis

redis-server
redis-server & #守护进程方式启动

nodejs-crawler's People

Contributors

lybenson avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.