Coder Social home page Coder Social logo

hapi_egghead's Introduction

Readme

This tutorial has been excellent, however I noticed straight away that the author was using es6 characters, const and let, as well as string interpolation, which might not be available in many environments at the time of this writing. I've set up a .babelrc file and added the Babel run-time to this project in order to access these features. Also, I recommend using Node 4.x LTS (currently Argon).

This course is currently free, so I assume posting my notes from the lesson is OK. However, if you're Mike Frey or anyone from Egghead, and don't approve of this repo, let me know.

Up and Running

  • Create a new npm project via npm init -- next install Hapi, npm -i hapi --save
  • add Nodemon, npm -i -g nodemon
  • tell Nodemon to watch the insertion point, nodemon -w ./ index.js

Create an index.js file

'use strict'
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({
  host: 'localhost',
  port: 8000
})

server.route({
  method: 'GET',
  path: '/',
  handler: (request, reply) => {
    reply('hello hapi')
  }
})

server.start(() => console.log(`Started at: ${server.info.uri}`))

Logging with Good and Good-Console

Hapi doesn't ship with logging support out of the box (other than basic console.log). However a bunch of plug-ins are supported:

  • Good (process monitor, listens for events from Hapi)
  • Good-Console (broadcasts events from Good)
  • See also good -file, -http, -udp (all officially supported by Hapi.js)
  • Also several community powered reporters (winston, influx, loggly, logstash, graylog2)

Route Parameters

A route's path can be specified with a variety of options. In one case, we have can give specific params to be returned, path: '/users', which will simply return an empty object when we request the '/users' path. If we want to return the userId for example, we can use path: '/users/{users}'. For flexibility, there is a wildcard parameter, path: '/files/{files*}' -- this will return anything requested from /files, for example '/files/cars/honda.jpg'.

Wildcard params can also be segmented, by appending a number to the * specifier. For example, /files/{files*3}, will accept three parameters -- 'a/b/c', and will throw an error if less than or more than the designated route segments are requested.

Hapi also supports partial segments. We can restrict matched params to specific file extensions like this, /files/{file}.jpg. Then, if we request /files/honda.jpg, the server will respond { file : honda }.

The teacher also takes time to mention "path specificity" - the fact that Hapi's router evaluates routes in order from "most specific" to "least specific". Which means that the order routes are created, does not affect the order in which they are evaluated. Therefore a most general route possible would look like, path: '/{stuff*}' -- any request that didn't match a more specific route, would fall through to this most general one.

hapi_egghead's People

Contributors

newswim avatar

Watchers

 avatar  avatar

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.