我在Express中设置了这样的路由器:
var r = require('rethinkdb');
var quotes = require('../model/quotes');
var user = require('../model/users');
var auth = require('../lib/auth');
var React=require('react');
var ReactDOMServer=require('react-dom/server')
var Homepage=React.createFactory(require('../component/index.js').Homepage);
module.exports = function(app, passport) {
app.get('/', function(req, res) {
if (req.user) {
res.redirect('/dashboard');
} else {
var HomepageHTML=ReactDOMServer.renderToString(Homepage({}));
res.render('index',{homepage:HomepageHTML});
}
});
}
index
页面将作为响应发送,以swig模板引擎
{% extends 'main.html' %}
{% block title %}Homepage{% endblock %}
{% block body %}
{{homepage}}
{% endblock %}
这是用React编写的Homepage
组件:
var React=require('react');
var Homepage=React.createClass({
render:function(){
return (
<div className="homepage-content">
<div className="left">
<img src={{'../public/images/img_landing_page_mac.png'}}/>
</div>
<div className="right">
<h2 style={{fontSize:'33px',letterSpacing:'5px'}}>
Organize <br/>Modern Knowledge<br/> for Mankind
</h2>
<p style={{fontSize:'20px',marginTop:'35px',letterSpacing:'4px'}}>
Consume, Collect and Revisit <br/>Knowledge at Your Fingertips
</p>
<a href="#"
style={{marginTop:'80px',display:'inline-block',marginLeft:'-17px'}}>
<img src={{'../public/images/btn_get_chrome.png'}}/>
</a>
</div>
</div>
);
}
});
exports.Homepage=Homepage;
当我启动服务器时,我遇到了这个错误并且徒劳无功地解决了它
/home/kuro/Workspace/corate/component/index.js:30
return (<div></div>);
^
SyntaxError: Unexpected token <
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/kuro/Workspace/corate/routes/index.js:7:34)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/kuro/Workspace/corate/app.js:43:1)
at Module._compile (module.js:413:34)
[nodemon] app crashed - waiting for file changes before starting...