博客
关于我
hapi.js入门系列(一)——一个简单的Hello World程序
阅读量:611 次
发布时间:2019-03-12

本文共 1739 字,大约阅读时间需要 5 分钟。

目录

简介

文章所使用各技术、框架版本一览

名称 版本
Node.js 12.15.0
hapi.js 20.1.0

一、安装hapi

1.1 创建项目

创建文件夹hapi-project,并进入该目录

# 创建文件夹mkdir hapi-project# 进入hapi-project文件夹cd hapi-project

1.2 初始化package.json

要使用package.json文件保管项目的依赖,需要先初始化一个package.json文件。

npm init

1.3 安装Hapi依赖

直接使用npm安装即可,当然也可以使用cnpm等其他包安装工具。

npm i @hapi/hapi --save

[注] 在以往版本的hapi,是直接使用 npm i hapi ,如果此处使用该命令,会默认安装18版本的hapi,要想安装20版本的hapi需要使用 npm i @hapi/hapi 命令。

二、一个简单的hello world程序

2.1 创建一个最基础的服务

hapi-project文件夹下,创建一个index.js文件,作为项目的入口文件。

index.js中编写创建服务的代码:

// 引入hapi依赖const hapi = require('@hapi/hapi');const start = async () => {     // 实例化一个server,并指定server的端口号  const server = hapi.server({    port: 3000 });  // 启动服务  await server.start();  console.log(`server is running at ${     server.info.uri}`);};// 执行start()方法start();

在终端输入如下命令,启动服务:

node ./index.js

启动后,终端会打印内容:server is running at ***:3000

我们的服务启动在3000端口,此时可以查看本机启动的端口已验证我们的服务是否正常启动:

  • Linux上:
netstat -tunlp | grep 3000
  • Windows上:
netstat -ano | findStr 3000

可以看到是有服务在监听0.0.0.0:3000的,此时就意味着服务已经正常启动了。

2.2 添加一个路由

在上面index.js的基础上,添加一个路由。

hapi的路由是通过server.route()方法进行添加的,相关代码如下:

// 引入hapi依赖const hapi = require('@hapi/hapi');const start = async () => {     // 实例化一个server,并指定server的端口号  const server = hapi.server({    port: 3000 });  // 注册路由  server.route({       method: 'GET',    path: '/',    handler: (request, h) => {         return 'hello world';    }  });  // 启动服务  await server.start();  console.log(`server is running at ${     server.info.uri}`);};// 执行start()方法start();

这里我们添加了一个路由,该路由的请求方法是GET方法,路由的path值是/handler中定义了路由的路由控制器。

[注]路由控制器就是路由的逻辑实现部分。一般多层架构还会将路由控制器进行更进一步的细化分层。

此时,浏览器访问127.0.0.1:3000就可以看到返回了内容hello world

[注] 在以前的版本中,路由的handler方法返回内容使用的是 reply 方法,在20版本中,是直接使用 return 关键字进行返回,直接将要返回的内容放在 return 关键字后即可。

转载地址:http://lfpxz.baihongyu.com/

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>