首页
/ Node URL Shortener 技术文档

Node URL Shortener 技术文档

2024-12-24 22:09:24作者:劳婵绚Shirley

1. 安装指南

1.1 环境准备

在开始安装之前,请确保您的系统已经安装了以下软件:

  • Node.js(建议使用最新稳定版本)
  • Redis(建议使用最新稳定版本)

1.2 克隆项目

首先,从GitHub克隆项目到本地:

$ git clone git@github.com:dotzero/node-url-shortener.git
$ cd node-url-shortener

1.3 安装依赖

进入项目目录后,安装项目所需的依赖包:

$ npm install

1.4 启动项目

安装完成后,可以通过以下命令启动项目:

$ node app

默认情况下,项目会在http://127.0.0.1:3000上运行。

2. 项目的使用说明

2.1 快速启动

如果您想快速启动项目,可以使用以下命令:

$ git clone git@github.com:dotzero/node-url-shortener.git
$ cd node-url-shortener
$ npm install
$ node app

2.2 命令行选项

项目支持一些命令行选项,您可以通过以下命令查看所有选项:

$ node app -h

常用选项包括:

  • -u, --url:应用程序的URL,默认为http://127.0.0.1:3000
  • -p, --port:Express应用程序的端口号,默认为3000
  • --redis-host:Redis服务器的主机名,默认为localhost
  • --redis-port:Redis服务器的端口号,默认为6379
  • --redis-pass:Redis服务器的密码,默认为空
  • --redis-db:Redis数据库的索引,默认为0

3. 项目API使用文档

3.1 创建短链接

通过POST /api/v1/shorten接口创建短链接。您可以通过表单数据传递long_url参数来指定要缩短的URL。其他可选参数包括start_dateend_datec_new

示例请求:

POST /api/v1/shorten
Content-Type: application/x-www-form-urlencoded

long_url=http://google.com

响应示例:

{
  "hash": "rnRu",
  "long_url": "http://google.com",
  "short_url": "http://127.0.0.1:3000/rnRu",
  "status_code": 200,
  "status_txt": "OK"
}

3.2 扩展短链接

通过GET /api/v1/expand/:hash接口获取短链接的详细信息。您可以通过URL中的:hash参数指定要查询的短链接。

示例请求:

GET /api/v1/expand/rnRu

响应示例:

{
    "start_date": "undefined",
    "end_date": "undefined",
    "hash": "rnRu",
    "long_url": "http://127.0.0.1:3000/rnRu",
    "clicks": "0",
    "status_code": 200,
    "status_txt": "OK"
}

4. 项目安装方式

4.1 开发环境安装

在开发环境中,您可以按照以下步骤安装和启动项目:

$ git clone git@github.com:dotzero/node-url-shortener.git
$ cd node-url-shortener
$ npm install
$ node app

4.2 生产环境安装

在生产环境中,您可以按照以下步骤安装和启动项目:

$ git clone git@github.com:dotzero/node-url-shortener.git node-url-shortener
$ cd node-url-shortener
$ npm install --production
$ NODE_ENV=production node app --url "http://example.com"

5. 测试

5.1 运行测试

要运行项目的测试套件,首先安装依赖,然后运行npm test

$ npm install
$ npm test

6. 许可证

本项目基于MIT许可证发布。

登录后查看全文
热门项目推荐