Impossibile distribuire l'applicazione React JS nel servizio app di Azure

Aug 19 2020

Stavo esaminando alcuni dei collegamenti per distribuire l'applicazione React Js sul servizio app di Azure. Ma sto affrontando qualche problema durante la distribuzione dell'applicazione.

Ho aggiunto tutte le cose necessarie come web.configil file alla publiccartella e ho anche aggiunto la directory di build all'area di lavoro.

Distribuzione dell'app Web nel servizio app di Azure

Distribuisci Node.js nel servizio app di Azure usando Visual Studio Code

ho seguito tutti i passaggi ma ottenendo un errore inferiore quando provo a eseguire la distribuzione sul servizio app di Azure. Prima di distribuire eseguo questi comandi

npm run build.

2020-08-19T10:44:45.762075166Z A P P   S E R V I C E   O N   L I N U X
2020-08-19T10:44:45.762079567Z 
2020-08-19T10:44:45.762083667Z Documentation: http://aka.ms/webapp-linux
2020-08-19T10:44:45.762088167Z NodeJS quickstart: https://aka.ms/node-qs
2020-08-19T10:44:45.762092268Z NodeJS Version : v12.16.3
2020-08-19T10:44:45.762096468Z Note: Any data outside '/home' is not persisted
2020-08-19T10:44:45.762100768Z 
2020-08-19T10:44:45.789282727Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-08-19T10:44:45.792738514Z Build Operation ID: |OtQwveNuO0A=.83a2ec6c_
2020-08-19T10:44:47.255197638Z Writing output script to '/opt/startup/startup.sh'
2020-08-19T10:44:47.960307930Z Running #!/bin/sh
2020-08-19T10:44:47.960336532Z 
2020-08-19T10:44:47.960345833Z # Enter the source directory to make sure the script runs where the user expects
2020-08-19T10:44:47.960355334Z cd "/home/site/wwwroot"
2020-08-19T10:44:47.960363235Z 
2020-08-19T10:44:47.960370735Z export NODE_PATH=$(npm root --quiet -g):$NODE_PATH
2020-08-19T10:44:47.960378436Z if [ -z "$PORT" ]; then
2020-08-19T10:44:47.960386136Z      export PORT=8080
2020-08-19T10:44:47.960393937Z fi
2020-08-19T10:44:47.960401238Z 
2020-08-19T10:44:47.960408638Z echo Found tar.gz based node_modules.
2020-08-19T10:44:47.960416339Z extractionCommand="tar -xzf node_modules.tar.gz -C /node_modules"
2020-08-19T10:44:47.960424040Z echo "Removing existing modules directory from root..."
2020-08-19T10:44:47.960431740Z rm -fr /node_modules
2020-08-19T10:44:47.960439141Z mkdir -p /node_modules
2020-08-19T10:44:47.960446542Z echo Extracting modules...
2020-08-19T10:44:47.960453842Z $extractionCommand
2020-08-19T10:44:47.960461243Z export NODE_PATH="/node_modules":$NODE_PATH
2020-08-19T10:44:47.960468943Z export PATH=/node_modules/.bin:$PATH
2020-08-19T10:44:47.960476344Z if [ -d node_modules ]; then
2020-08-19T10:44:47.960483745Z     mv -f node_modules _del_node_modules || true
2020-08-19T10:44:47.960491245Z fi
2020-08-19T10:44:47.960498446Z 
2020-08-19T10:44:47.960505546Z if [ -d /node_modules ]; then
2020-08-19T10:44:47.960524748Z     ln -sfn /node_modules ./node_modules 
2020-08-19T10:44:47.960532849Z fi
2020-08-19T10:44:47.960540149Z 
2020-08-19T10:44:47.960547550Z echo "Done."
2020-08-19T10:44:47.960554951Z npm start
2020-08-19T10:44:48.258132115Z Found tar.gz based node_modules.
2020-08-19T10:44:48.258154316Z Removing existing modules directory from root...
2020-08-19T10:44:48.260461807Z Extracting modules...
2020-08-19T10:44:48.262765098Z tar (child): node_modules.tar.gz: Cannot open: No such file or directory
2020-08-19T10:44:48.262778299Z tar (child): Error is not recoverable: exiting now
2020-08-19T10:44:48.262970515Z tar: Child returned status 2
2020-08-19T10:44:48.262983816Z tar: Error is not recoverable: exiting now
2020-08-19T10:44:48.290740216Z Done.
2020-08-19T10:44:48.512406278Z npm info it worked if it ends with ok
2020-08-19T10:44:48.512836614Z npm info using [email protected]
2020-08-19T10:44:48.512976126Z npm info using [email protected]
2020-08-19T10:44:48.578204629Z npm info lifecycle [email protected]~prestart: [email protected]
2020-08-19T10:44:48.584464048Z npm info lifecycle [email protected]~start: [email protected]
2020-08-19T10:44:48.589867495Z 
2020-08-19T10:44:48.589881796Z > [email protected] start /home/site/wwwroot
2020-08-19T10:44:48.589887297Z > react-scripts start
2020-08-19T10:44:48.589891697Z 
2020-08-19T10:44:48.597331914Z sh: 1: react-scripts: not found
2020-08-19T10:44:48.598224588Z npm info lifecycle [email protected]~start: Failed to exec start script
2020-08-19T10:44:48.599091959Z npm ERR! code ELIFECYCLE
2020-08-19T10:44:48.599182267Z npm ERR! syscall spawn
2020-08-19T10:44:48.599258573Z npm ERR! file sh
2020-08-19T10:44:48.599314678Z npm ERR! errno ENOENT
2020-08-19T10:44:48.600738196Z npm ERR! [email protected] start: `react-scripts start`
2020-08-19T10:44:48.600749897Z npm ERR! spawn ENOENT
2020-08-19T10:44:48.600754497Z npm ERR! 
2020-08-19T10:44:48.600758798Z npm ERR! Failed at the [email protected] start script.
2020-08-19T10:44:48.600763398Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-19T10:44:48.605436585Z npm timing npm Completed in 125ms
2020-08-19T10:44:48.605621800Z 
2020-08-19T10:44:48.605672405Z npm ERR! A complete log of this run can be found in:
2020-08-19T10:44:48.605750311Z npm ERR!     /root/.npm/_logs/2020-08-19T10_44_48_601Z-debug.log

Ma il fatto è che la stessa applicazione funziona perfettamente in locale con i comandi seguenti npm install, npm start.

e solo per verificare se la build generata dopo npm run buildfunziona o meno ha provato a eseguire l'applicazione dalla directory di build con i comandi seguenti

npm install -g serve

poi

serve -s build

quindi l'applicazione si apre nel browser.

Risposte

2 rav Aug 19 2020 at 21:58

Dal registro sembra che questa non sia Node.jsun'applicazione, ma reactun'applicazione. Pertanto react-scripts startè qualcosa di cui il servizio app di Azure non sa nulla.

Quando esegui reactun'app su localhost, è alimentata da un development serverche in effetti è un Node.jsserver, ma una volta creata per la produzione utilizzandola npm run buildnon è altro che un index.htmlfile alimentato da una serie di .jsfile e fogli di stile. Non ha funzionalità web in sé.

served'altra parte è una storia a parte. Secondo la loro descrizione su npmjs.com: Supponendo che desideri servire un sito statico, un'applicazione a pagina singola o solo un file statico (non importa se sul tuo dispositivo o sulla rete locale), questo pacchetto è la scelta giusta per te.

Ma questo non è un approccio simile ad Azure.

In produzione, tuttavia, se si usa Azure, si consiglia di usare Archiviazione BLOB di Azure v2, che dispone di funzionalità di hosting di siti statici. Abilitare l'hosting di siti statici nell'archivio BLOB e distribuire la buildcartella in un contenitore denominato $web. Ovviamente tutto questo viene fatto automaticamente se si utilizza vscodecon Azure plugin. Supponendo che tu abbia effettuato l'accesso ad Azure tramite vscode, fai clic con il pulsante destro del mouse sulla buildcartella e seleziona deploy to static site, segui i passaggi e sarai live con la tua app react.

Tuttavia, se disponi di un Node.js expressback-end accanto reactall'app, puoi inserire la buildcartella nel Node.jsprogetto allo stesso livello della node_modulescartella e utilizzare il routing statico per far funzionare sia il front-end che il back-end come un unico pacchetto. Definisci esplicitamente un percorso per indicare expressdi rispondere con il index.htmlfile quando richiesto. Quindi puoi distribuire l'intero pacchetto in un file Azure App Service.

const express = require('express');
const path = require('path');
const port = process.env.PORT || 3006;

const app = express();

app.use(express.json())
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({extended: true}));

app.use(express.static(__dirname + '/build'));
app.get('/', (req, res) => { 
  res.sendFile(path.join(__dirname + '/build/index.html'))
});

// Then prefix your API endpoints with /api
app.get('/api/user/:id', (req, res) => {
  // Code to get user by id
});

app.post('/api/user', (req, res) => {
  // Code to save user
});

app.listen(port, () => {
  console.log(`App bootstrapped on port ${port}...`);
});

Quando /viene colpito, index.htmlviene servito. Le chiamate API servite come definito con /api/*. Trovo questo meccanismo utile molte volte.

Buona fortuna.

2 PrateekNaik Aug 20 2020 at 15:33

Dopo aver fatto qualche ricerca su Google, ho trovato la soluzione . Dobbiamo aggiungere il comando seguente nel comando di avvio nella configurazione del servizio app per le macchine Linux.

pm2 serve /home/site/wwwroot --no-daemon

Passi:

- Go to App Service

- Navigate to Configuration

- Click on General Settings

- add the above command in Start up command, click on save 

- then restart the server