目標。配置應用程序,其中包括:React,Webpack和MongoDB。Webpack中的獨立客戶端和服務器
因此,我已經爲React設置了Webpack並嘗試導入Mongoose。問題:React客戶端和Mongoose - 服務器端,並且因爲Webpack必須具有兩者的配置。使用這個答案:https://stackoverflow.com/a/37391247/7479176我試圖配置Webpack。之後,我嘗試在我的server.jsx文件中導入Mongoose,但它不起作用。
問題。如何配置Webpack,以便我可以使用MongoDB?
編輯。我想出瞭如何擺脫警告(見警告):
output: {
filename: 'bundle.node.js',
libraryTarget: 'commonjs',
path: path.resolve(__dirname, 'dist')
},
externals: [
/^(?!\.|\/).+/i
],
但是,當我添加代碼到server.jsx(見server.jsx),它並沒有在控制檯登錄的消息。
的WebPack配置:
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
module.exports = [
{
entry: {
index: './src/app/app.jsx'
},
devtool: 'inline-source-map',
devServer: {
port: 3000,
host: 'localhost',
historyApiFallback: true,
noInfo: false,
stats: 'minimal',
hot: true, // Tell the dev-server we're using HMR
contentBase: path.resolve(__dirname, './dist'),
publicPath: '/'
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(), // Enable HMR
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'body'
})
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
}
},
{
entry: {
index: './src/server/server.jsx'
},
target: 'node',
output: {
filename: 'bundle.node.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
}
}
]
server.jsx:
import mongoose from 'mongoose'
import '../config/database.js'
mongoose.Promise = global.Promise
mongoose.connect(config.database)
// check connection
mongoose.connection.once('open',() => console.log('Connected to MongoDB'))
// check for db errors
mongoose.connection.on('error', err => console.log(err))
警告:
WARNING in ./node_modules/mongoose/lib/drivers/index.js
10:13-49 Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/require_optional/index.js
82:18-42 Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/require_optional/index.js
90:20-44 Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/require_optional/index.js
97:35-67 Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/es6-promise/dist/es6-promise.js
Module not found: Error: Can't resolve 'vertx' in 'D:\Projects\JavaScriptProjects\pizzaday\node_modules\es6-promise\dist'
@ ./node_modules/es6-promise/dist/es6-promise.js 131:20-30
@ ./node_modules/mongodb/lib/mongo_client.js
@ ./node_modules/mongodb/index.js
@ ./node_modules/mongoose/lib/index.js
@ ./node_modules/mongoose/index.js
@ ./src/server/server.jsx
恕我直言,客戶端和服務器真的應該在單獨的項目中,或者至少在單獨的文件夾層次結構中,如果你堅持把兩者都放在同一個根項目文件夾中。就個人而言,我總是保持後端分離,並且僅僅在webpack開發服務器的代理選項中傳遞代理選項以在開發期間「通過」到單獨運行的服務器。在現實生活中,正面和背面經常會有部署週期的解耦。所以也有這個考慮。 –