我正在尝试使用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它创建了一个构建文件夹。也%public_url%字符串由幕后的一些纱线脚本取代。因此,我们无法直接提供该文件夹。相反,我们必须生成using build。
create-react-app
build
%public_url%
使用yarn build或npm run build。这将生成包含build文件夹asset,manifest和其他文件。
yarn build
npm run build
asset
manifest
之后,使用该build文件夹静态提供文件以供生产使用。