makemehapi
Version:
Self guided workshops to teach you about hapi.
52 lines (39 loc) • 1.56 kB
Markdown
Create a server which responds to requests to `/` with a static HTML file named
`index.html` containing the following:
```html
<html>
<head><title>Hello Handling</title></head>
<body>
Hello Handling
</body>
</html>
```
-----------------------------------------------------------------
##HINTS
This exercise requires you to install the `/inert` module, which is a hapi
plugin for serving static files and directories. You'll need to register the
plugin in your code in order to serve static files:
```js
var Inert = require('/inert');
await server.register(Inert);
```
You can declare handlers as objects instead of functions. The object must
contain one of the following: `file` (requires `/inert` plugin), `directory`
(requires `/inert` plugin), `proxy` (requires `@hapi/h2o2` plugin), or
`view` (requires `/vision` plugin).
For example, `handler` can be assigned an object with the `file` key:
```js
handler: {
file: "index.html"
}
```
Be careful: in practice, you'll need to provide an absolute path to an
`index.html` file in your program's directory. To achieve this, you'll probably
need the `path` core module, its `join()` function, and the `__dirname` global
variable.
You can also configure a files base path in your server and just pass relative
paths in the route. This is specially useful if you have multiple routes that
respond with files.
## Docs
- Hapi - Route options: https://hapi.dev/api#route-options
- Hapi - Serving Static Content Tutorial: https://hapi.dev/tutorials/servingfiles