小编szn*_*brt的帖子

在Express中为客户端路由提供多个反应应用程序

我为单个服务提供了不同的软件产品,需要将其部署到单个服务器上.通过create-react-app进行构建设置,客户端构建为react,而服务器运行Node.js和Express.

当我从服务器提供单个应用程序时,它按以下方式完成:

// App.js
// ...
// Entry point for data routes (API)
app.use('/data', indexRoute);

if(process.env.NODE_ENV !== 'development') {
  app.use(express.static(path.join(__dirname, 'build-client')));

  app.get('/*', function(req, res) {
    return res.sendFile(path.resolve( __dirname, 'build-client' , 'index.html'));
  });
}
Run Code Online (Sandbox Code Playgroud)

我希望能够从服务器提供多个应用程序.我该怎么办?

我尝试的是为资产连接不同的静态路径,并使用不同的名称分隔客户端,尽管它不起作用.像这样:

// App.js
// ...
// Entry point for data routes (API)
app.use('/data', indexRoute);

if(process.env.NODE_ENV !== 'development') {
  app.use(express.static(path.join(__dirname, 'build-client')));
  app.use(express.static(path.join(__dirname, 'build-admin')));

  app.get('/client/*', function(req, res) {
    return res.sendFile(path.resolve( __dirname, 'build-client' , 'index.html'));
  });
  app.get('/admin/*', function(req, res) {
    return res.sendFile(path.resolve( __dirname, 'build-client' , 'index.html'));
  });
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过这种方式,但Express …

node.js express reactjs

9
推荐指数
2
解决办法
4426
查看次数

React + Webpack捆绑 - 如果排除了node_modules,则"浏览器中未定义require"

我正在学习React,我正在尝试将它与Webpack一起使用,但我遇到了以下问题:

如果我使用此webpack配置,则不会排除节点模块,捆绑过程需要20秒,捆绑包的大小超过2MB(请参阅下面的CLI输出):

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: [
    'bootstrap-loader',
    './src/index.js',
    'style!css!./src/style/style.css'
  ],
  output: {
    path: path.resolve(__dirname + 'build'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      { test: /\.jsx?$/,
        loader: 'babel-loader'
      },
      {
        test: /\.scss$/,
        loaders: [
          'style',
          'css?modules&importLoaders=2&localIdentName=[name]__[local]__[hash:base64:5]',
          'postcss',
          'sass',
        ],
      },
      {
        test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "url?limit=10000"
      },
      {
        test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
        loader: 'file'
      },
      // Bootstrap 3
      { test:/bootstrap-sass[\/\\]assets[\/\\]javascripts[\/\\]/, loader: 'imports?jQuery=jquery' },
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

CLI输出1

... 在此输入图像描述

但是,如果我将以下两行添加到我的配置并使用nodeExternals,则捆绑包变小并且运行得很快,虽然它不起作用,因为在浏览器中我收到错误'未捕获的ReferenceError:require未定义':

  ...
  target: 'node', // …
Run Code Online (Sandbox Code Playgroud)

bundle reactjs webpack

5
推荐指数
1
解决办法
3813
查看次数

JSON.parse()上的数值更改

我正在使用Node/Express向非官方Vine API发出API请求.

GET https://api.vineapp.com/users/search/路由返回的数据在解析时发生更改.

我的代码如下:

request({ url: 'https://api.vineapp.com/users/search/' + username }, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(typeof body,'UNPARSED BODY:', body);

  body = JSON.parse(body);

  console.log(typeof body,'PARSED BODY:', JSON.stringify(body, null, 2));

  cb(null, body)
});
Run Code Online (Sandbox Code Playgroud)

这是什么回报: 在此输入图像描述 data.records.userId在解析时发生更改.

为什么会这样?我在这里错过了什么吗?他们为什么要那样做?

javascript parsing json node.js vine

1
推荐指数
1
解决办法
1602
查看次数

标签 统计

node.js ×2

reactjs ×2

bundle ×1

express ×1

javascript ×1

json ×1

parsing ×1

vine ×1

webpack ×1