百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

sample-hapi-rest-api

> 编程语言
开源

在 hapi.js 框架上构建 REST API 的示例结构

194 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 hapi.js 框架上构建 REST API 的示例结构

This project is outdated. We've built it with Hapi.js v3.0 and Node v0.10.26.
There is a another similar example working with newer versions here: https://github.com/rcorral/hapi-restful-api-example

Sample hapi.js REST API

A sample structure for building a REST API on top of hapi.js framework that consumes a MySQL database.


Start by cloning this repo and going inside the project's folder:

bash
$ git clone [email protected]:agendor/sample-hapi-rest-api.git
$ cd sample-hapi-rest-api

To begin with, we have to define some environment variables (with an Ubuntu 12.04 you can edit the ~/.bashrc file):

bash
export NODE_ENV=development
export NODE_HOST=localhost
export NODE_PORT=8000
export DB_DEV_USER=root
export DB_DEV_PASS=rootpass

PS: Don't forget to alter DB_DEV_USER AND DB_DEV_PASS values properly to access your MySQL Database.

Then you have to give permission to the script database.sh:

bash
$ chmod +x database.sh

After giving permission, you can run it. It's going to drop and then re-create a MySQL Database called hapi-todo with user and task tables and insert a default user, which credentials are:

  • Email: [email protected]
  • Pass: 123

So run it:

bash
$ bash database.sh

Database setup. So it's time to install project dependencies, run the following command:

bash
$ npm install

Now you can check the recently created database in you MySQL. Start the server by running grunt in the terminal:

bash
$ grunt

All the unit tests will run and everything should be green.

That's it! You should be able to play around your API GETting, POSTing, PUTing AND DELETEing Tasks. The end-points are as described in the src/routes/task.js file:

  • GET http://localhost:8000/tasks/{task_id}
  • GET http://localhost:8000/tasks
  • POST http://localhost:8000/tasks
  • PUT http://localhost:8000/tasks/{task_id}
  • DELETE http://localhost:8000/tasks/{task_id}

Further reading

If you want a deeper analysis of the project, checkout this Gist.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言