简体   繁体   中英

Node.js isomorphic-react-example error

I saw this https://github.com/DavidWells/isomorphic-react-example and create another simple one,but the error message is can't find module app,is it miss something?Or how should I change the code?thanks

server.js

var express = require('express');
var exphbs  = require('express-handlebars');

var app = express();

require("node-jsx").install();
var React = require("react");
 App = React.createFactory(require("app"));


app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

app.get('/', function (req, res) {
    var markup = React.renderToString(App());  
    res.render('main', { 
     title: 'Express',
    markup: markup 
  });
});


app.listen(3000);

app.js

 var React = require("react");


var App = React.createClass({
  getInitialState() {
    return {
      search: ""
    };
  },
  render() {
    return (
      <div className="search-component">
        <input type="text" onChange={this.changeSearch} />
        <p><span>You are searching for: {this.state.search}</span></p>
      </div>
    );
  },
  changeSearch(event) {
    var text = event.target.value;

    this.setState({
      search: text
    });
  }
});


module.exports = App;

main.handlebars

 <!DOYCYPE html>
<html>
<head>
<script>
var as = "as";
</script>

<body>
<p> main </p>
{{{markup}}}
</body>
</html>

If you want to require a file as a module you have to give it the pathname, in this case ./app if it is in the same directory.

So your line

App = React.createFactory(require("app"));

Should become something like

var App = React.createFactory(require("./app"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM