如何在hapi 0.8.4中添加路由
How to add a route in hapi 0.8.4
我有一个小问题与这个简单的代码启动服务器与nodejs和Hapi。这是代码:
var Hapi = require('hapi');
var http = new Hapi.Server('0.0.0.0', 8080);
http.route({
method: 'GET',
path: '/api',
handler: function(request, reply) {
reply({ 'api' : 'hello!' });
}
}
);
http.start();
,这是错误:
http.route({
^
TypeError: undefined is not a function
at Object.<anonymous> (C:'Users'Prova.js:8:6)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
这是一个非常基础的代码,但我不明白为什么它有http.route的问题。
在hapi 0.8.4中,您可以使用addRoute()
添加路由:
var Hapi = require('hapi');
// Create a server with a host and port
var server = new Hapi.Server('localhost', 8000);
// Define the route
var hello = {
handler: function (request) {
request.reply({ greeting: 'hello world' });
}
};
// Add the route
server.addRoute({
method: 'GET',
path: '/hello',
config: hello
});
// Start the server
server.start();
但是那个版本的hapi太旧了,你应该升级到最新的。当前hapi的稳定版本是8.8.0
相关文章:
- 如何在 html 页面上动态添加路由到脚本 src
- 流星 - 添加带有熨斗的路由后无法提交用户.路由器到登录和注册表单
- Nodejs-使用regex将多个参数添加到路由中
- Ember教程-向router.js添加新的路由
- Ember添加了对转换调用上下文(路由/控制器)的检查
- 动态添加/注册AngularJS控制器,无需路由
- 为什么我的 AngularJS 应用程序在添加路由时不起作用
- Nodejs 快速路由:如果添加了“/”,则没有样式
- 路由不起作用 AngularJS,只是添加一个哈希
- Express :如何将中间件独占添加到路由器对象中的大型路由列表中
- 如何在运行时将路由或状态添加到 Ember.Router
- 主干:从视图类向路由器添加路由
- 用javascript中的iron路由器动态添加路由
- 运行时添加路由(ExpressJs)
- 向遗留网站添加路由
- 添加路由提供程序时出现未捕获对象错误
- 向Backbone.js路由器添加路由,包括404处理程序
- 如何在hapi 0.8.4中添加路由
- Extjs 5-如何在init上添加路由
- 以编程方式添加路由后,当访问任何路由时,angular 会在按下后退按钮时引发错误