express + webpack + react 配置

相关配置文件

  • package.json
{
  "name": "micandlyle",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "css-loader": "^0.28.4",
    "express": "^4.15.3",
    "extract-text-webpack-plugin": "^2.1.2",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "style-loader": "^0.18.2",
    "webpack": "^3.0.0",
    "webpack-dev-server": "^2.4.5"
  },
  "devDependencies": {
    "webpack-dev-middleware": "^1.10.2",
    "webpack-hot-middleware": "^2.18.0"
  }
}
  • webpack.config.js
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

var publicPath = 'http://localhost:3000/'; // 确保图片地址引用有效。
var hotMiddlewareScript = 'webpack-hot-middleware/client?reload=true';  //reload = true 参数,如果碰到不能hot reload的情况,就整页刷新。

var config = {
    entry:['./main', hotMiddlewareScript],

    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'index.js',
        publicPath: publicPath
    },
    devtool: 'source-map',

    // devServer: {
    //     inline: true,
    //     port: 3000
    // },

    module: {
        loaders: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
                presets: ['es2015', 'react', 'stage-0']
            }
        },
        {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader'})
      },
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),   // webpack-dev-middleware & webpack-hot-middleware 插件配置
        new webpack.NoEmitOnErrorsPlugin(),
        new ExtractTextPlugin("[name].css") // 单独为页面引入css
    ]
}

/** javascript related to entry will hot reload */
if(module.hot) {
    module.hot.accept();
}

module.exports = config;
  • server.js
    var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// var cookieParser = require('cookie-parser');
var path = require('path');

/** webpack middleware config, 在routes之前 */
var webpack = require('webpack'),
    webpackDevMiddleware = require('webpack-dev-middleware'),
    webpackHotMiddleware = require('webpack-hot-middleware'),
    webpackDevConfig = require('./webpack.config.js');
var compiler = webpack(webpackDevConfig);
// attach to the compiler & the server
app.use(webpackDevMiddleware(compiler, {

    // public path should be the same with webpack config
    publicPath: webpackDevConfig.output.publicPath,
    noInfo: true,
    stats: {
        colors: true
    }
}));
app.use(webpackHotMiddleware(compiler));

// app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.urlencoded({extended: true})); //通常 POST 内容的格式是 application/x-www-form-urlencoded
// app.use(cookieParser);
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'public/index.html'));
});

app.listen(3000,() => {
    console.log('App is running on port 3000');
});
  • main.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'))
  • App.js
import React from 'react';
class App extends React.Component {
    render() {
        return (
            <div>
                Hello World!!!
            </div>
        );
    }
} export default App;
  • public/index.html
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>React App</title>
</head>

<body>
    <div id="app"></div>
    <script src="index.js"></script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值