带有节点Express的create-react-app获得%PUBLIC_URL% [英] create-react-app with node express getting %PUBLIC_URL%

查看:425
本文介绍了带有节点Express的create-react-app获得%PUBLIC_URL%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Express服务器创建-react-app。设置服务器后,当我点击请求时,我会得到

I'am trying create-react-app with express server. after setting server when I hit request I'm getting

获取http:// localhost:3333 /%PUBLIC_URL%/ favicon.ico 400(错误的请求)

在错误预览中,它给了我

On error preview its giving me

URIError: Failed to decode param '/%PUBLIC_URL%/favicon.ico'
    at decodeURIComponent (<anonymous>)
    at decode_param (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/layer.js:172:12)
    at Layer.match (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/layer.js:123:27)
    at matchLayer (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/index.js:574:18)
    at next (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/index.js:220:15)
    at jsonParser (/home/owaishanif/code/flashcard-app/node_modules/body-parser/lib/types/json.js:103:7)
    at Layer.handle [as handle_request] (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/index.js:317:13)
    at /home/owaishanif/code/flashcard-app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/owaishanif/code/flashcard-app/node_modules/express/lib/router/index.js:335:12)






此处服务器代码是


Here is the Server Code

var express = require('express');
var bodyParser = require('body-parser');
var path = require ('path');

var data = {};

express()
    .use(express.static(path.resolve(__dirname, '..', 'public')))
    .use(bodyParser.json())

    .get('/api/data', (req, res) => res.json(data))

    .post('/api/data', (req, res) => res.json(data = req.body))

    .get('*', (req, res) => res.sendFile( path.resolve( __dirname, '..', 'public/index.html')))

    .listen(3333, function(){
        console.log('server running at 3333');
    });

我想在服务器上使用create react app。有在线文章,但它们已过时。欢迎使用帮助提示和技巧。

I want to use create react app with server. There are articles online but they are outdated. Help tips and tricks are welcome.

推荐答案

我已经使用 create-react-app解决了此问题 build 创建一个构建文件夹。 %public_url%字符串也被幕后的一些毛线脚本代替。因此,我们无法直接提供该文件夹。相反,我们必须使用 build 生成。

I have solved this using create-react-app build which creates a build folder. also %public_url% string is replaced by some yarn scripts behind the scenes. So we cant directly serve that folder. Instead we have to generate using build.

使用纱线构建 npm运行构建。这将生成一个包含资产清单和其他文件的构建文件夹。

Use yarn build or npm run build. That will generate a build folder containing asset, manifest, and other files.

之后,使用该build文件夹为您的文件静态提供供生产使用。

After that, use that build folder to statically serve your files for production use.

这篇关于带有节点Express的create-react-app获得%PUBLIC_URL%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆