webpack + express + angular 4 error“错误:ENOENT:没有这样的文件或目录,stat'C:\ public \ index.html'

时间:2018-04-28 05:18:31

标签: javascript node.js angular express webpack

我的应用程序的目录结构 -

- Project-root
 - client-side-angular-code // angular 4 code
 - public // generated angular 4 static assets including index.html
 - dist
    - bundle.js //generated bundle file by webpack for express code 
    - public //copied static assets from the ../public to dist/public by webpack
 - app.js //express app
 - webpack.config.js
 - package.json
 - .... other miscellaneous files and folders

使用angular“bg build”我将所有静态资源放入公共文件夹,如上所述,在我的项目的根目录中。 现在使用快速应用程序,我可以通过index.html从公共文件夹提供静态资源,并在运行“node app.js”并浏览到http://localhost:port时正常工作  相关代码在这里 -

app.use(express.static(path.join(__dirname, 'public')));
app.get('*', function (req, res) {


res.sendFile("./index.html", { root: path.join(__dirname, './public') })
})

但在我将用于生产部署的app.js以及剩余的静态资产捆绑到dist / public文件夹(我使用“CopyWebpackPlugin”复制静态资源)之后,如果我尝试运行“node dist / bundle.js“并尝试以与以前相同的方式浏览到http://localhost:port,我收到了以下错误。

  

错误:ENOENT:没有这样的文件或目录,stat'C:\ public \ index.html'       在错误(本机)

因此我想知道为什么webpack不会相对于bundle.js的位置和/或我做错了什么?

相关的webpack配置文件

    var path = require("path");
const webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

    module.exports = {

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname,"dist"),
    //path: __dirname + '/dist',
    publicPath : path.resolve(__dirname,"dist/")
  },
    entry: "./app",
    target: 'node',
    module: {
        rules: [
          { test: /\.json/, loader: "json-loader", exclude:/node_modules/,},
          {test: /\.ts(x?)$/, loader: 'babel-loader!ts-loader',exclude: /node_modules/,},
          { test: /\.css$/, loaders: ['to-string-loader', 'css-loader'] },
          { test: /\.html$/, loader: 'raw-loader' },
          {
            test: /\.(jsx|js)?$/,
            use: [{
              loader: "babel-loader",
              options: {
                cacheDirectory: true,
                presets: ['es2015'] // Transpiles JSX and ES6
              }
            }]
          }
        ],
      },
    resolve: {
        extensions: ['.js', '.ts', '.html', '.css']
    },
    plugins: [
        new CopyWebpackPlugin([
            {from:'public',to:'public'} 
        ]),
        new CleanWebpackPlugin(['dist']),
    ]
};

这是我的webpack版本的package.json条目(并非所有模块都在使用中,我们可以从上面的webpack配置中看到) -

 "clean-webpack-plugin": "^0.1.19",
    "copy-webpack-plugin": "^4.5.1",
    "css-loader": "^0.28.11",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "raw-loader": "^0.5.1",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.0.15",
    "webpack-dev-middleware": "^3.1.3",
    "webpack-hot-middleware": "^2.22.1",
    "webpack-node-externals": "^1.7.2"

非常感谢任何帮助或建议!!

1 个答案:

答案 0 :(得分:1)

在弄清楚问题后回答我自己的问题。不得不改为这个 - res.sendFile(process.cwd()+"./public/index.html");