簡體   English   中英

帶有節點Express的create-react-app獲得%PUBLIC_URL%

[英]create-react-app with node express getting %PUBLIC_URL%

我正在嘗試使用Express服務器創建反應應用程序。 設置服務器后,當我點擊請求時

GET http://localhost:3333/%PUBLIC_URL%/favicon.ico 400 (Bad Request)

在錯誤預覽上,它給了我

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)

這是服務器代碼

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。 有在線文章,但它們已過時。 歡迎使用幫助提示和技巧。

我已經使用create-react-app build解決了這個問題,它創建了一個build文件夾。 還有%public_url%字符串被幕后的一些毛線腳本代替。 因此,我們無法直接提供該文件夾。 相反,我們必須使用build來生成。

使用yarn buildnpm run build 這將生成一個包含assetmanifest和其他文件的構建文件夾。

之后,使用該build文件夾靜態提供文件以供生產使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM