有没有一种方法可以将BrowserSync与Express服务器一起使用。我正在使用Cory House的React-slingshot入门套件,并且使用BrowserSync来提供文件。但是,我对使用Express提供文件感兴趣。
我尝试创建一个新的server.js
文件并添加以下代码:
/* eslint-disable no-console */
import express from 'express';
import http from 'http';
import path from 'path';
const app = express();
const server = http.createServer(app);
const port = 3000;
app.use(express.static(path.resolve(__dirname, '../src/*.html')));
server.listen(port, () => {
console.log(`Express Dev Server started on port ${port}`);
});
浏览器同步配置如下:
// This file configures the development web server
// which supports hot reloading and synchronized testing.
// Require Browsersync along with webpack and middleware for it
import browserSync from 'browser-sync';
// Required for react-router browserHistory
// see https://github.com/BrowserSync/browser-sync/issues/204#issuecomment-102623643
import historyApiFallback from 'connect-history-api-fallback';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from '../webpack.config.dev';
const bundler = webpack(config);
// Run Browsersync and use middleware for Hot Module Replacement
browserSync({
port: 3000,
ui: {
port: 3001
},
server: {
baseDir: 'src',
middleware: [
historyApiFallback(),
webpackDevMiddleware(bundler, {
// Dev middleware can't access config, so we provide publicPath
publicPath: config.output.publicPath,
// These settings suppress noisy webpack output so only errors are displayed to the console.
noInfo: true,
quiet: false,
stats: {
assets: false,
colors: true,
version: false,
hash: false,
timings: false,
chunks: false,
chunkModules: false
},
// for other settings see
// https://webpack.js.org/guides/development/#using-webpack-dev-middleware
}),
// bundler should be the same as above
webpackHotMiddleware(bundler)
]
},
// no need to watch '*.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
files: [
'src/*.html'
]
});
然后,我修改了package.json以同时启动包含browserSync配置的文件和包含express js的文件。但是由于某种原因,我得到了该端口已在使用中的错误。
我知道这是因为browserSync正在使用该端口,但是有一种方法可以同时运行browserSync和Express JS。
谢谢。