webpack和fluxible的新手,所以我试图了解他们的入门样板:http://fluxible.io/quick-start.html
运行npm run dev
运行正常并启动实时重载网络服务器,但我对如何在生产中运行它感到困惑。看看package.json
"scripts": {
"build": "webpack & webpack --config webpack.config.production.js",
"dev": "node webpack-dev-server.js & PORT=3001 nodemon start.js -e js,jsx",
"lint": "eslint ./*.js ./**/*.js",
"start": "node start.js"
}
我假设我会运行npm run build
,它似乎在生产配置模式下运行webpack并将js文件复制到/build
文件夹。此时如果我运行npm start,它会运行start.js
,它只指向server.js
并且服务器正在运行而没有开发热负载。
我的问题是:为什么应用程序仍在继续进行轮询(我假设的是套接字或轮询以运行dev hot-laoder)当它正在运行我假设处于生产模式时。我在日志中看到了这一点:
express:router dispatching GET /socket.io/?EIO=3&transport=polling&t=1445788884250-772 +3s
express:router query : /socket.io/?EIO=3&transport=polling&t=1445788884250-772 +1ms
express:router expressInit : /socket.io/?EIO=3&transport=polling&t=1445788884250-772 +0ms
express:router compression : /socket.io/?EIO=3&transport=polling&t=1445788884250-772 +0ms
express:router jsonParser : /socket.io/?EIO=3&transport=polling&t=1445788884250-772 +0ms
在生产服务器上,我只是部署整个根文件夹,或者我应该只部署构建文件夹......如果是这样,构建文件夹就会丢失。
静态资产是否应该被复制到build文件夹中 - 就像express中的/ public文件夹一样?
start.js
require('babel/register');
module.exports = require('./server');
server.js
import express from 'express';
import compression from 'compression';
import bodyParser from 'body-parser';
import path from 'path';
import serialize from 'serialize-javascript';
import {navigateAction} from 'fluxible-router';
import debugLib from 'debug';
import React from 'react';
import ReactDOM from 'react-dom/server';
import app from './app';
import HtmlComponent from './components/Html';
import { createElementWithContext } from 'fluxible-addons-react';
const env = process.env.NODE_ENV;
const debug = debugLib('fluxible');
const server = express();
server.use('/public', express.static(path.join(__dirname, '/build')));
server.use(compression());
server.use(bodyParser.json());
server.use(async (req, res, next) => {
const context = app.createContext();
debug('Executing navigate action');
try {
await context.getActionContext().executeAction(navigateAction, {
url: req.url
});
debug('Exposing context state');
const exposed = 'window.App=' + serialize(app.dehydrate(context)) + ';';
debug('Rendering Application component into html');
const markup = ReactDOM.renderToString(createElementWithContext(context));
const htmlElement = React.createElement(HtmlComponent, {
clientFile: env === 'production' ? 'main.min.js' : 'main.js',
context: context.getComponentContext(),
state: exposed,
markup: markup
});
const html = ReactDOM.renderToStaticMarkup(htmlElement);
debug('Sending markup');
res.type('html');
res.write('<!DOCTYPE html>' + html);
res.end();
} catch (err) {
if (err.statusCode && err.statusCode === 404) {
// Pass through to next middleware
next();
} else {
next(err);
}
}
});
const port = process.env.PORT || 3000;
server.listen(port);
console.log('Application listening on port ' + port);
export default server;