SoFunction
Updated on 2024-11-20

TypeScript development HapiJS application details

Initialize the npm project

yarn init

Adding Dependencies

yarn add hapi

Adding Development Dependencies

To use theTypeScripAt the same time, there needs to be at least one tool that can always listen for changes to the project files and update the changes in real time to the started service, I chose to use theNodemonThe first step is to add the following development dependencies

yarn add typescript -D
yarn add nodemon -D

Next, we need to set up thenode together withhapi Install the type definition library:

yarn add @types/node -D
yarn add @types/hapi -D

After the installation is complete, the The file looks like the following:

{
  "name": "hapiserver",
  "version": "0.0.1",
  "description": "API server",
  "main": "",
  "author": "Your Name",
  "license": "MIT",
  "dependencies": {
    "hapi": "^18.1.0"
  },
  "devDependencies": {
    "@types/hapi": "^18.0.2",
    "@types/node": "^12.0.2",
    "nodemon": "^1.19.0",
    "typescript": "^3.4.5"
  }
}

Note: Yourdependencies together withdevDependencies The version number may be different from mine in the configuration.

Configuring TypeScript

Designing the project file directory structure

In the root directory of the project, create a file namedsrc directory to contain all of the system's source code files, then create a directory nameddist directory, which is used to hold the data provided by thetypescript compiledjavascript Documentation.

Note: the file structure is not mandatory, you can follow your own habits and specifications completely

.
├── dist
├── node_modules
├── 
├── src
└── 

TypeScript will query the file named configuration file to find the project's entry files and compile settings. Detailed instructions on how to use it can be obtained from the/docs/handbook/ Check, where we first fill in the following:

{
  "compilerOptions": {
    "outDir": "./dist",
    "allowJs": false,
    "target": "es6",
    "sourceMap": true,
    "module": "commonjs",
    "moduleResolution": "node"
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules"]
}

exist file, we define theoutDir The value of the./dist, which tells the compiler that the compiled output directory is./dist folder, you can now compile it by executing the following code directly from the project root foldersrc directory to thedist directory of the JavaScript file.

node_modules/typescript/bin/tsc

Developing Hapi Service Applications with TypeScript

existsrc directory, create a file named The document, which reads as follows:

import * as hapi from "hapi";
// Create a server that listens to the `8000` commodity on `localhost`.
const server:  = new ({
  host: "localhost",
  port: 8000
});
// Add Route
({
  method: "GET",
  path: "/hello",
  handler: function(request, h) {
    return "Hello! TypeScript!";
  }
});
// Start the service
async function start() {
  try {
    await ();
  } catch (err) {
    (err);
    (1);
  }
  ("Server running at:", );
}
// Don't forget to start the service
start();

Since our code is written in TypeScript, we don't have a way to run it directly yet, we need to compile it into JavaScript before we can run it:

Use the following command to compile the code:

node_modules/typescript/bin/tsc

After compiling, you will get two files like the following:

dist
├── 
└── 

At this point, execute the following code to start the service:

node dist/

After successful startup, the terminal will display:

Server running at: http://localhost:8000

utilizationcurl Test our services:

$ curl -i http://localhost:8000/hello
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
cache-control: no-cache
content-length: 18
accept-ranges: bytes
Date: Fri, 17 May 2019 01:58:50 GMT
Connection: keep-alive
Hello! TypeScript!

It's already started successfully.

Complete all configurations

We can't just manually perform a compilation and restart the service every time we change the code, we can do it in the Add two commands to the

{
  ...
  "scripts": {
    "start": "./node_modules/nodemon/bin/ -e ts  --exec \"yarn run compile\"",
    "compile": "tsc && node ./dist/"
  },
  ...
}

Now, simply execute the following code in the project root directory to start a development environment that compiles code in real time and automatically re-services it:

yarn start

It works:nodemon Start a service that listens for file changes, and after any file changes, execute theyarn run compile command (i.e., execute.tsc && node ./dist/to restart the service.

TypeScript development HapiJS application is detailed above, more information about TypeScript development HapiJS please pay attention to my other related articles!