TypeError: response is not a function

Using Hapi v17, I'm just trying to make a simple web API to start building my knowledge, but I always get an error every time I test the created GET methods. Below is the code I'm running:

'use strict';
const Hapi = require('hapi');
const MySQL = require('mysql');

//create a serve with a host and port

const server = new Hapi.Server({
   host: 'serverName',
   port: 8000
});

const connection = MySQL.createConnection({
     host: 'host',
     user: 'root',
     password: 'pass',
     database: 'db'
});

connection.connect();

//add the route
server.route({
   method: 'GET',
   path: '/helloworld',
   handler: function (request, reply) {
   return reply('hello world');
}
});

server.start((err) => {
   if (err) {
      throw err;
   }
   console.log('Server running at:', server.info.uri);
});

The following is the error:

Debug: internal, implementation, error 
    TypeError: reply is not a function
    at handler (/var/nodeRestful/server.js:26:11)

I'm not sure why there is a problem with calling the response function, but right now this is a fatal error.

+11
source share
3 answers

Hapi version 17 has a completely different API.

https://hapijs.com/api/17.1.0

reply , , Response Toolkit, , .
API Response Toolkit , , :

//add the route
server.route({
  method: 'GET',
  path: '/helloworld',
  handler: function (request, h) {
    return 'hello world';
  }
});

Response Toolkit , , . :

  ...
  handler: function (request, h) {
    const response = h.response('hello world');
    response.type('text/plain');
    return response;
  }

: API server.start() , , ( , console.log() ). server.start() , .

, API async-await.

+19

, return reply('hello world'); return 'hello world'; :

hapi v17.x reply() :

  • response.hold() response.resume().

  • , .

  • (h) ( reply()).
+2

It seems you have duplication in your code:

const server = new Hapi.Server({
   host: 'serverName',
   port: 8000
});

// Create a server with a host and port
// This second line is not needed!!! and probably is causing the error 
//you described
const server = new Hapi.Server();
0
source

All Articles