2

Website Collection

 2 years ago
source link: http://kevinnan.org.cn/index.php/archives/691/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

本项目是基于nodejs的网页收藏工具,前端使用layui框架,后端使用koa2框架,前端通过ajax向后端发起数据请求(GET/POST)。
项目地址:Kevinnan-teen/website-collection;
项目演示:wsc.nachr.top

Usage

调试代码(建议本地安装nodejs环境)

  • 在本地安装npm(8.1.2),配置npm国内下载源,使用npm安装node(16.13.2),进入项目根目录,执行npm install命令安装nodejs依赖包;
  • 进入models目录:
    • 创建数据库和数据表(网站信息、子页面信息、网站分类信息、用户信息);
      node create_table.js
      
    • 创建用户名和密码,注意将username和password替换为自己的用户名和密码;
      node create_user_info.js username password
      
  • 进入项目根目录,运行程序;
    node app.js
    
  • 在浏览器端用http://ip:3000访问项目主页;

部署运行(建议使用docker)

  • 使用已经配置好的Dockerfile创建image镜像文件:
    docker image build -t website-collection:1.0.0 .
    
  • 用刚刚创建的image生成容器,并让容器在后台运行
    docker container run -p 8000:3000 -dt website-collection:1.0.0
    
  • 查看正在运行的容器,复制容器ID
    docker container ls
    
  • 进入在后台运行的容器。
    docker container exec -it [containerID] /bin/bash
    
  • 进入models目录:
    • 创建数据库和数据表(网站信息、子页面信息、网站分类信息、用户信息);
      node create_table.js
      
    • 创建用户名和密码,注意将username和password替换为自己的用户名和密码;
      node create_user_info.js username password
      
  • 进入项目根目录,让程序在后台运行;
    node app.js &
    
  • 在浏览器端用http://ip:3000访问项目主页;
  • 后续集成,为了方便更新在docker中运行的项目代码,我们可使用git来持续更新
    • 将本地的代码变动提交到GitHub上;
    • web服务器端把执行git pull命令拉取最新代码;
    • 在项目跟目录下执行docker cp命令,更新容器中的目录/文件
    docker container cp . [container/ID]:[dir]
    

后端使用Koa2搭建,用到以下Koa2中间件模块:

  • koa-bodyparser:解析GET/POST请求
  • koa-static:支持静态资源访问
  • koa-views:渲染页面
  • ejs:提供koa-views的ejs渲染支持
  • koa-router:页面/API路由
  • koa-session:支持cookie/session
  • koa-passport:支持页面鉴权访问
  • passport-local:使用用户名和密码来进行验证登录

nodejs使用其他模块:

  • crypto:密码加密
  • sqlite3:提供sqlite3数据库API接口

前端使用Layui搭建,用到以下模块:

  • 导航 nav
  • 表单 form
  • 数据表格 table
  • 时间线 timeline

项目目录结构:

  • app.js:代码入口
  • models:封装数据库CURD操作
  • routers:页面和API路由
  • controllers:控制器存放目录,接收路由请求,处理逻辑
  • views:视图页面文件(ejs、html)
  • public:静态资源(css、js、img)
  • utils
  • test:测试代码

Bug & Todo

  • 表 website_info(website_title, website_url, website_class_1, website_class_2, website_detail) 和表 website_class_info(website_class_child, website_class_abbrativation, website_class_parent, website_class_detail) 设计错误,修改website_class_info的class字段不会影响website_info的class数据,
    这导致对网站分类的修改删除对网站数据不能产生影响,因为分类数据在website_info中是早已写好的

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK