GithubHelp home page GithubHelp logo

hapiblog's Introduction

📕 hapiblog 博客 api

基于 hapi 框架的博客系统,使用 jwt 鉴权,支持用户注册和登录,获取文章列表和文章详情。以 redis 作为缓存解决方案,使用 mysql 数据库和 sequelize 处理数据持久化。

👕 DEMO

点击这里查看DEMO

🌈 支持的功能

  • 分页获取博客文章列表
  • 增查改删博客文章
  • 根据标签、标题和作者 ID 筛选文章
  • 获取文章排行列表支持 30 天以内的文章阅读量排行
  • 使用 redis 缓存请求的数据
  • 用户的注册和登录以及 JSON WebToken 的鉴权
  • 通过 swagger 创建 API 文档
  • 使用 hapi-good 日志工具
  • 使用 sequelize 创建数据迁移

screencapture-127-0-0-1-8000-documentation-2018-10-07-21_48_37.png

🎉 使用方法

🍎 clone

git clone https://github.com/oliyg/hapiblog.git

🚨 配置环境变量

根目录新增 .env 文件,仿照 .env.example 配置环境变量:

# server
HOST = HOST
PORT = PORT

# mysql
MYSQL_USERNAME = MYSQL_USERNAME
MYSQL_PASSWORD = MYSQL_PASSWORD
MYSQL_DB_NAME = MYSQL_DB_NAME
MYSQL_HOST = MYSQL_HOST
MYSQL_PORT = MYSQL_PORT

# redis
REDIS_HOST = REDIS_HOST
REDIS_PORT = REDIS_PORT

# jwt
JWT_SECRET = JWT_SECRET

# passwd encrypt secret
PASSWD_SECRET = PASSWD_SECRET

📖 数据迁移

开启 mysql 服务,并执行命令:

  • npm run createdb:dev 创建数据库
  • npm run createtable 创建数据库表
  • npm run initdata 创建示例数据
  • npm run start 开启服务

✈️ 启动服务

访问 http://127.0.0.1:8000/documentation#/ 查看 API 文档

hapiblog's People

Contributors

oliyg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hapiblog's Issues

npm run start 报错,在mac下,本地错误如下?

npm run start

image

===============
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13
/Users/jerry/demo/hapiblog/node_modules/hoek/lib/index.js:740
throw new Error(msgs.join(' ') || 'Unknown error');
^

Error: Invalid connection options {
"compression": true,
"router": {
"isCaseSensitive": true,
"stripTrailingSlash": false
},
"routes": {
"cache": {
"statuses": [
200,
204
],
"otherwise": "no-cache"
},
"compression": {},
"cors": false,
"files": {
"relativeTo": "."
},
"json": {
"replacer": null,
"space": null,
"suffix": null,
"escape": false
},
"log": false,
"payload": {
"failAction": "error",
"maxBytes": 1048576,
"output": "data",
"parse": true,
"timeout": 10000,
"uploads": "/var/folders/3n/15_qj3ts07b4wm928grxy3700000gn/T",
"defaultContentType": "application/json",
"compression": {}
},
"response": {
"ranges": true,
"emptyStatusCode": 200,
"options": {}
},
"security": null,
"state": {
"parse": true,
"failAction": "error"
},
"timeout": {
"server": false
},
"validate": {
"options": {}
}
},
"host": "HOST",
"port" [1, 2, 3]: "PORT"
}

[1] "port" must be a number
[2] "port" with value "PORT" fails to match the required pattern: ///
[3] "port" with value "PORT" fails to match the required pattern: /^\\.\pipe\/
at Object.exports.assert (/Users/jerry/demo/hapiblog/node_modules/hoek/lib/index.js:740:11)
at Object.exports.apply (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/schema.js:17:10)
at concat.forEach (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/server.js:165:27)
at Array.forEach ()
at module.exports.internals.Server.internals.Server.connection (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/server.js:159:24)
at Object. (/Users/jerry/demo/hapiblog/app.js:18:8)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...

=========================

npm run start,页面报错!

npm run start

image

===========================

sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules/sequelize/lib/sequelize.js:242:13
/Users/jerry/demo/hapiblog/node_modules/hoek/lib/index.js:740
throw new Error(msgs.join(' ') || 'Unknown error');
^

Error: Invalid connection options {
"compression": true,
"router": {
"isCaseSensitive": true,
"stripTrailingSlash": false
},
"routes": {
"cache": {
"statuses": [
200,
204
],
"otherwise": "no-cache"
},
"compression": {},
"cors": false,
"files": {
"relativeTo": "."
},
"json": {
"replacer": null,
"space": null,
"suffix": null,
"escape": false
},
"log": false,
"payload": {
"failAction": "error",
"maxBytes": 1048576,
"output": "data",
"parse": true,
"timeout": 10000,
"uploads": "/var/folders/3n/15_qj3ts07b4wm928grxy3700000gn/T",
"defaultContentType": "application/json",
"compression": {}
},
"response": {
"ranges": true,
"emptyStatusCode": 200,
"options": {}
},
"security": null,
"state": {
"parse": true,
"failAction": "error"
},
"timeout": {
"server": false
},
"validate": {
"options": {}
}
},
"host": "HOST",
"port" [1, 2, 3]: "PORT"
}

[1] "port" must be a number
[2] "port" with value "PORT" fails to match the required pattern: ///
[3] "port" with value "PORT" fails to match the required pattern: /^\\.\pipe\/
at Object.exports.assert (/Users/jerry/demo/hapiblog/node_modules/hoek/lib/index.js:740:11)
at Object.exports.apply (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/schema.js:17:10)
at concat.forEach (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/server.js:165:27)
at Array.forEach ()
at module.exports.internals.Server.internals.Server.connection (/Users/jerry/demo/hapiblog/node_modules/hapi/lib/server.js:159:24)
at Object. (/Users/jerry/demo/hapiblog/app.js:18:8)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...

作者您好,项目是如何部署到阿里云

作者您好,我也正在学习hapi,写完了,准备部署到服务器上去,自己有一台阿里云的服务器,安装了nodejs,git 查了一堆资料,说是把项目git clone 到服务器上,然后运行 node app.js 。但是我运行node app.js 报错了。现在有点懵逼中,查了一堆资料,有点无从下手的感觉。刚发了封邮件给您,希望您有空说下服务器部署的思路,在此万分感谢

hapi-swagger Failed to load API definition.

{
    "hapi-swagger": "^10.3.0",
    "inert": "^5.1.3",
    "package": "^1.0.1",
    "vision": "^5.4.4"
}
const hapiSwagger = require("hapi-swagger");
const inert = require("inert");
const vision = require("vision");
const package = require("package");

module.exports = [
  inert,
  vision,
  {
    plugin: hapiSwagger,
    options: {
      info: {
        title: "接口文档", // 文档的标题
        version: package.version, // 文档的版本
        description: '基于 hapi 框架的后台管理系统',
        contact: {
          name: 'Oliver',
          url: 'https://github.com/oliyg',
          email: '[email protected]',
        },
        license: {
          name: 'MIT',
          url: 'https://github.com/oliyg/hapiblog/blob/master/LICENSE',
        }
      },
      lang: 'zh-cn', // 文档语言
      // 定义接口以tags属性为分类【定义分类的大标题】,给./routes路由的配置config:tags使用
      grouping: "tags",
      // 标签,用于对应路由config定义的tags进行归类
      tags: [
        { name: "tests", description: "测试相关" }
        // { name: "users", description: "测试相关" }
      ]
    }
  }
];


const init = async ()=> {

	const server = new Hapi.Server({
		port: config.port,
		host: config.host,
		routes: {
			files: {
				relativeTo: Path.join(__dirname, 'public')
			},
			cors: {
				origin: ['*']
			}
		}
	})

  	await server.register([
	    // 为系统使用 hapi-swagger
	    ...pluginHapiSwagger,
  	]);

	// 路径接口
	Routes.forEach((api)=>{
		server.route(api)
	});
	
	// 日志
	// let log = new Log(server)
	// await log.register()
	// await log.pino()

    await server.start()
    console.info(`Server started at ${server.info.uri}`)
    await server.inject('/')
};

init()

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.