我正在構建一個React應用程序w/Express。我的服務器工作正常,我的Webpack創建我的js文件沒有問題,但由於某種原因我的應用程序不呈現。爲什麼不快速渲染我的React應用程序?
這裏是我的測試程序:
import React from 'react';
import { render } from 'react-dom';
import Application from './Application';
console.log('works!');
function TestApp({}) {
return (
<div className="TestApp">Does this work?</div>
);
}
render(<TestApp />, document.getElementById('root'));
這裏是我的html:
<!DOCTYPE html>
<html>
<head>
<title>React Test App</title>
</head>
<body>
<div id="root" />
<script src="js/bundle.js" async />
</body>
</html>
這裏是我的webpack.config.js
文件:
const path = require('path');
const webpack = require('webpack');
module.exports = {
context: path.resolve(__dirname, './app/assets/js/'),
entry: './bundle',
output: {
path: path.resolve(__dirname, './public/assets/js/'),
filename: 'bundle.js',
publicPath: "/js/",
},
resolve: {
extensions: ['', '.js', '.jsx', '.json']
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
],
module: {
preLoaders: [{
test: /\.(js|jsx)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
}],
loaders: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
}, {
test: /\.css$/,
loader: 'style!css',
}, ]
},
watch: true
};
,這裏是我的中間件:
var express = require('express.io');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var path = require('path');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
app.set('view engine', 'ejs');
app.use(express.compress());
app.use(express.static(path.join(__dirname, './public/assets')));
app.set('views', path.join(__dirname, './app/views'));
router = express.Router();
router.get("*", (req, res) => {
res.render("index.ejs");
});
app.use("/", router);
app.listen(port, function (error) {
return error
? console.error(error)
: console.info("Listening on port %s", port);
});
任何線索我可能做錯了什麼?
它可以是類型res.render(「index.ejs」);我認爲它應該是「index.js」 – fayzaan
這取決於您在index.ejs文件中提供的html嗎? –
只有真正的1337 h4xx0rs可以使用反應。 – r3wt