无法在Node JS中显示背景图片

时间:2018-11-07 17:52:44

标签: javascript webpack

我正在构建一个简单的网页来进行测试,我必须使用node.js服务器进行测试。我可以像这样通过src轻松添加图像:

<img src="assets/young-woman.jpg">

但是,当我尝试执行此操作时:

style="background-image: url(assets/young-woman.jpg);"

...不起作用。

我已尝试遵循各种解决方案来解决此问题,例如:https://github.com/webpack-contrib/css-loader/issues/256和此Webpack - background images not loading,但没有一种解决方案对我有用。

如果我通过CSS这样操作,它也可以正常工作:

.element {
    background-image: url(../assets/young-woman.jpg);
}

很明显我的webpack.config文件肯定有问题吗?

const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');
const path = require('path');

module.exports = {
    entry: {
        app: './src/index.js',
    },
    devServer: {
        contentBase: './dist'
    },
    devtool: "source-map",
    module: {
        rules: [
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [{
                        loader: "css-loader", options: {
                            sourceMap: true
                        }
                    }, {
                        loader: "sass-loader", options: {
                            sourceMap: true
                        }
                    }],
                    publicPath: '../',
                })
            },
            {
                test: /\.(png|jp(e*)g|gif)$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'img/'
                    }
                }]
            },
            {
                test: /\.(html)$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        attrs: [':src']
                    }
                }
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env']
                    }
                }
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: "css/[name].[contenthash].css",
        }),
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
            template: 'src/index.html',
            inject: 'body',
            filename: 'index.html'
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            Popper: ['popper.js', 'default'],
            Util: "exports-loader?Util!bootstrap/js/dist/util",
            Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
        })
    ],
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: ''
    }
}

当我尝试在浏览器中加载资源时,我只会得到Cannot GET /assets/young-woman.jpg

我已经尝试安装express,但是这对我也不起作用,并且出现有关 fs 的错误。我似乎无法解决这个问题。我的设置是否有问题?

预先感谢

0 个答案:

没有答案