如何加载从webpack-dev-middleware生成的js并启动js

时间:2018-01-29 16:43:38

标签: node.js express webpack webpack-dev-middleware webpack-hot-middleware

我是Node JS的新手。我正在使用webpack-hot-middleware创建一个热重新加载的Backend API服务器。我尝试使用webpack-dev-middleware将服务器脚本(index.js)加载到内存并执行该javascrip文件。

以下是我的代码:link

我发现我的代码可以成功进行热重装,但是我无法加载服务器脚本(index.js)并启动我的API服务器。出于某些原因,我想使用webpack-dev-middleware而不是webpack dev server。

这是我的文件结构:

MediaType::class

我在前端或后端服务器上发现了大量有关如何执行该操作的教程,但是我需要启动我的index.js。

所以,我的问题是如何加载从webpack-dev-middleware生成的js文件并启动该js文件。

以下是我尝试的内容:

**PostType:**


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('media', MediaType::class, array(
                     'provider' => 'sonata.media.provider.image',
                     'context'  => 'post'
            ));
    }

在webpack.devserver.config.js中:

├── package.json.
├── build
|   ├── devserver.js
|   └── webpack.devserver.config.js
├── server
|   └── index.js (code of the API server)
└── dist

结果:在我更改了index.js上的内容之后,代码似乎已成功重新加载,但是当我访问/ api / hi API时,我看不到更改。

const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.devserver.config');
var StreamCache = require('stream-cache');
var fs = require('fs');

const compiler = webpack(webpackConfig);

const app = express();

const hotMiddleware = webpackHotMiddleware(compiler);
const middleware = webpackMiddleware(compiler, {
    publicPath: "",
    contentBase: 'server',
    stats: {
        colors: true,
        hash: false,
        timings: true,
        chunks: false,
        chunkModules: false,
        modules: false
    }
});
app.use(middleware);
app.use(hotMiddleware);

middleware.waitUntilValid(() => {
    //Then, what should I do??????
  console.log(middleware.fileSystem.readFileSync(path.join(__dirname,               '../dist/backend.js')));
})

0 个答案:

没有答案