我想使用Express,pg並對我的項目做出反應。但反應給我一些問題。React - Syntaxerror:unexpected token <
index.js
var express = require('express');
var server = express();
var path = require('path');
var app = require('./app/app.js');
server.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/public/index.html'));
});
server.listen(3000, function() {
console.log("server is running on port 3000!");
});
var connexionDB = require('./db/connexionAvecPg');
app.js
var react = require('react');
var {render} = require('react-dom');
var App = react.createClass({
render: function() {
return(
<div>
<p>test</p>
</div>
);
}
});
render(
<App/>,
document.getElementById('root')
);
(是的,它是ES5,但我有我的理由使用它,而不是ES6-7)
我的代碼有什麼問題?我忘記了什麼嗎? Thx爲您的時間。
--- \編輯/ ---
明很快的回答後,我嘗試這樣做:
app.js
var React = require('react');
var ReactDOM = require('react-dom');
class App extends React.component{
render() {
return React.createElement('div', null, 'Test');
}
};
ReactDOM.render(
React.createElement(App, null),
document.getElementById('root')
);
但我有這樣的:
/home/josue/im-expressandpg/app/app.js:14
class App extends React.component{
^
TypeError: Class extends value undefined is not a constructor or null
--- \ EDIT 2/---
Thx to PSo我看到了我的錯誤。我改變了這一點:
class App extends React.component{
要這樣:
class App extends React.Component{
當我前高管:
/home/josue/im-expressandpg/app/app.js:17
document.getElementById('root')
^
ReferenceError: document is not defined
如果我補充一點:
var document = require('./../public/index.html');
我得到:
/home/josue/im-expressandpg/public/index.html:1
(function (exports, require, module, __filename, __dirname) { <!doctype html>
^
SyntaxError: Unexpected token <
使用大寫字母C表示:React.Component – PSo