node.js - Hapijs-react-views 设置

标签 node.js reactjs hapi.js

我发现按照 hapijs-react-views 包设置指南 ( npm hapi-js-react-views ) 遇到一些困难。

我可以运行服务器,但我只在 localhost:3000 上收到此错误

{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}

我在 github 上的仓库是:hapi-react GitHub

我的代码是:

-routes
--index.js
-views
--index.jsx
-app.js
-package.js

//routes/index.js

exports.index = function(request, reply){
  reply.view('index', { name: 'John' });
};

//views/index.js

var HelloMessage = React.createClass({
   render: function() {
      return <div>Hello {this.props.name}</div>;
   }
});

module.exports = HelloMessage;

//app.js

var hapi = require('hapi');
var vision = require('vision');
var path = require('path');
var engine = require('hapijs-react-views')();

// Create a server with a host and port
var server = new hapi.Server();
server.connection({ 
    host: 'localhost', 
    port: 3000 
});

// Register Hapi plugins
server.register(vision, function (err) {
  if(err) throw err;
});

var options = { jsx: { harmony: true } };
server.views({
    defaultExtension: 'jsx',
    engines: {
        jsx: require('hapijs-react-views')(options), // support for .jsx files
        js: require('hapijs-react-views')(options) // support for .js
    },
    relativeTo: __dirname,
    path: 'views'
});

// Add the route
server.route({
    method: 'GET',
    path: '/',
    config: {
        handler: require('./routes').index
    }
});

// Start the server
server.start((err) => {

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

//package.json

  "dependencies": {
    "hapi": "^13.4.1",
    "hapijs-react-views": "^0.7.3",
    "react": "^15.1.0",
    "vision": "^4.1.0"
  }

你能帮我吗? 提前致谢。

最佳答案

对于将来看到这一点的任何人,这里有一个使用 vision 渲染 jsx 的工作示例:https://github.com/hapijs/vision/tree/master/examples/jsx

关于node.js - Hapijs-react-views 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522850/

相关文章:

node.js - NodeJS 0.10.46 在lighttpd代理之后不再提供服务

javascript - 访问组件内的函数?

javascript - 在 HapiJS Confidence 配置对象中设置过滤对象的默认值

hapi.js - 使用 Hapi 18.1 延迟响应

javascript - 使用Express JS进行API外部CALL(避免cors问题)

javascript - 如何使用 node.js 按顺序列出目录的内容?

javascript - mochawesome 报告的名称未按预期保存(即使 cypress.json 中的 'reportFilename' 已更改)

node.js - 如何在Electron-Forge中成功高效地安装SQLite3?

javascript - 在 Json 中搜索过滤器

node.js - CORS 不适用于 hapijs 和 socket.io