<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Aplicaci%C3%B3_Reactjs_a_trav%C3%A9s_del_servidor_web_Express</id>
	<title>Aplicació Reactjs a través del servidor web Express - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Aplicaci%C3%B3_Reactjs_a_trav%C3%A9s_del_servidor_web_Express"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Aplicaci%C3%B3_Reactjs_a_trav%C3%A9s_del_servidor_web_Express&amp;action=history"/>
	<updated>2026-08-30T10:23:40Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=Aplicaci%C3%B3_Reactjs_a_trav%C3%A9s_del_servidor_web_Express&amp;diff=259200&amp;oldid=prev</id>
		<title>Joan: /* Tutorial */</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Aplicaci%C3%B3_Reactjs_a_trav%C3%A9s_del_servidor_web_Express&amp;diff=259200&amp;oldid=prev"/>
		<updated>2020-04-09T15:01:54Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Tutorial&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;___TOC___&lt;br /&gt;
=Tutorial=&lt;br /&gt;
Seguim&lt;br /&gt;
*https://dev.to/loujaybee/using-create-react-app-with-express&lt;br /&gt;
&lt;br /&gt;
La idea és que quan desenvolupem una app amb React fem:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm run start (o npm start)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
i la visulitzem pel port 3000:&lt;br /&gt;
*http://localhost:3000&lt;br /&gt;
i el punt d'entrada d'aqueta app és el script ''src/App.js''. Aquest seria el mode de desenvolupament.&lt;br /&gt;
&lt;br /&gt;
Ara bé, en producció hem de servir l'app a través d'un servidor web (Express) per un altre port (per ex 8080), i el punt d'entrada ha de ser el script ''index.html'' (''public/index.html'' i ''build/index.html'').&lt;br /&gt;
&lt;br /&gt;
Anem a veure com fer-ho.&lt;br /&gt;
===Step 1: Install create-react-app===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ create-react-app your-app-name&lt;br /&gt;
$ cd your-app-name&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Step 2: Install packages for create react app===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm install; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Podem comprovar que funciona fent:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm start&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*localhost:3000&lt;br /&gt;
El punt d'entrada és ''src/App.js''.&lt;br /&gt;
===Step 2b: npm run build===&lt;br /&gt;
Ara podem desplegar aquesta aplicació:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm run build&lt;br /&gt;
&lt;br /&gt;
&amp;gt; your-app-name@0.1.0 build /home/joan/M06_WEC_1920/UF2/your-app-name&lt;br /&gt;
&amp;gt; react-scripts build&lt;br /&gt;
&lt;br /&gt;
Creating an optimized production build...&lt;br /&gt;
Compiled successfully.&lt;br /&gt;
&lt;br /&gt;
File sizes after gzip:&lt;br /&gt;
&lt;br /&gt;
  39.39 KB  build/static/js/2.ed50ce24.chunk.js&lt;br /&gt;
  777 B     build/static/js/runtime-main.7a59697d.js&lt;br /&gt;
  651 B     build/static/js/main.6d4ec6a5.chunk.js&lt;br /&gt;
  547 B     build/static/css/main.5f361e03.chunk.css&lt;br /&gt;
&lt;br /&gt;
The project was built assuming it is hosted at /.&lt;br /&gt;
You can control this with the homepage field in your package.json.&lt;br /&gt;
&lt;br /&gt;
The build folder is ready to be deployed.&lt;br /&gt;
You may serve it with a static server:&lt;br /&gt;
&lt;br /&gt;
serve -s build&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ serve -s build&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I funciona:&lt;br /&gt;
*localhost:5000&lt;br /&gt;
El punt d'entrada és '''build/index.html'''&lt;br /&gt;
&lt;br /&gt;
Si fem:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ serve -s public&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
El punt d'entrada és '''public/index.html''' (editem aquest fitxer)&lt;br /&gt;
&lt;br /&gt;
'''NOTA''': el script ''build/index.html'' és el mateix que ''public/index.html''. En el moment que fem el ''build'' es copia ''public/index.html'' a ''build/index.html''.&lt;br /&gt;
===Step 3: Install express===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm install express --save&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Step 4: Create a server.js file===&lt;br /&gt;
script '''server.js''': (es crea a l'arrel del projecte, ''cd your-app-name'')&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
const express = require('express');&lt;br /&gt;
const bodyParser = require('body-parser')&lt;br /&gt;
const path = require('path');&lt;br /&gt;
const app = express();&lt;br /&gt;
app.use(express.static(path.join(__dirname, 'build')));&lt;br /&gt;
&lt;br /&gt;
app.get('/ping', function (req, res) {&lt;br /&gt;
 return res.send('pong');&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.get('/', function (req, res) {&lt;br /&gt;
  res.sendFile(path.join(__dirname, 'build', 'index.html'));&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.listen(process.env.PORT || 8080);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Step 5: Update your package.json===&lt;br /&gt;
Add the following to your package.json&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;proxy&amp;quot;: &amp;quot;http://localhost:8080&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queda de la següent manera:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
...&lt;br /&gt;
  &amp;quot;scripts&amp;quot;: {&lt;br /&gt;
    &amp;quot;start&amp;quot;: &amp;quot;react-scripts start&amp;quot;,&lt;br /&gt;
    &amp;quot;build&amp;quot;: &amp;quot;react-scripts build&amp;quot;,&lt;br /&gt;
    &amp;quot;test&amp;quot;: &amp;quot;react-scripts test&amp;quot;,&lt;br /&gt;
    &amp;quot;eject&amp;quot;: &amp;quot;react-scripts eject&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;proxy&amp;quot;: &amp;quot;http://localhost:8080&amp;quot;,&lt;br /&gt;
...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
===Step 6: Start the express server===&lt;br /&gt;
Arrenquem el servidor web:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ node server.js&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I l'aplicació la tenim disponible a:&lt;br /&gt;
*localhost:8080&lt;br /&gt;
El punt d'entrada és el script '''build/index.html'''. Això ho podem comprovar si modifiquem aquest fitxer(posem alguna cosa després del ''body''). Si volem que el punt d'entrada sigui '''public/index.html''' hem de modificar les línies ''app.use'' i ''res.sendFile'' del ''server.js''.&lt;br /&gt;
&lt;br /&gt;
'''NOTA'''. Això no cal fer-ho. ''public/index.html'' i ''build/index.html''. El primer es copia en el segon quan fem el ''npm run build''.&lt;br /&gt;
===Step 7. Conclusions===&lt;br /&gt;
Ara tinc un entorn de producció a través del port 8080.&lt;br /&gt;
&lt;br /&gt;
Puc continuar desenvolupant fent:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm start&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
i puc modificar el script ''App.js'' i veure els canvis en calent pel port ''localhost:3000''. Ara bé, no puc esperar veure aquests canvis pel port 8080. Per veure els canvis he de fer el ''npm run build''.&lt;br /&gt;
&lt;br /&gt;
=Aplicació pràctica: Simple example of a ReactJS and OpenLayers map component=&lt;br /&gt;
*https://dominoc925.blogspot.com/2019/10/simple-example-of-reactjs-and.html&lt;br /&gt;
Anem a posar-ho en pràctica i anem a fer el ''Hola món'' de Openlayers per visualitzar un mapa.&lt;br /&gt;
&lt;br /&gt;
Installing OpenLayers for ReactJS:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm install ol --save&lt;br /&gt;
$ npm install proj4 @material-ui/core --save&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Si mirem ''package.json'', veiem quines són les dependències, i crec que també és important fer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm install react --save&lt;br /&gt;
$ npm install react-dom --save&lt;br /&gt;
$ npm install react-scripts --save&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Creem el script ''src/js/components/OLMapFragment.js'': (aquest és el component principal de OpenLayers)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import React from 'react'&lt;br /&gt;
import Grid from '@material-ui/core/Grid'&lt;br /&gt;
&lt;br /&gt;
// Start Openlayers imports&lt;br /&gt;
import { &lt;br /&gt;
    Map,&lt;br /&gt;
    View&lt;br /&gt;
 } from 'ol'&lt;br /&gt;
import {&lt;br /&gt;
    GeoJSON,&lt;br /&gt;
    XYZ&lt;br /&gt;
} from 'ol/format'&lt;br /&gt;
import {&lt;br /&gt;
    Tile as TileLayer,&lt;br /&gt;
    Vector as VectorLayer&lt;br /&gt;
} from 'ol/layer'&lt;br /&gt;
import {&lt;br /&gt;
    Vector as VectorSource,&lt;br /&gt;
    OSM as OSMSource,&lt;br /&gt;
    XYZ as XYZSource,&lt;br /&gt;
    TileWMS as TileWMSSource&lt;br /&gt;
} from 'ol/source'&lt;br /&gt;
import {&lt;br /&gt;
    Select as SelectInteraction,&lt;br /&gt;
    defaults as DefaultInteractions&lt;br /&gt;
} from 'ol/interaction'&lt;br /&gt;
import { &lt;br /&gt;
    Attribution,&lt;br /&gt;
    ScaleLine,&lt;br /&gt;
    ZoomSlider,&lt;br /&gt;
    Zoom,&lt;br /&gt;
    Rotate,&lt;br /&gt;
    MousePosition,&lt;br /&gt;
    OverviewMap,&lt;br /&gt;
    defaults as DefaultControls&lt;br /&gt;
} from 'ol/control'&lt;br /&gt;
import {&lt;br /&gt;
    Style,&lt;br /&gt;
    Fill as FillStyle,&lt;br /&gt;
    RegularShape as RegularShapeStyle,&lt;br /&gt;
    Stroke as StrokeStyle&lt;br /&gt;
} from 'ol/style'&lt;br /&gt;
&lt;br /&gt;
import { &lt;br /&gt;
    Projection,&lt;br /&gt;
    get as getProjection&lt;br /&gt;
 } from 'ol/proj'&lt;br /&gt;
&lt;br /&gt;
// End Openlayers imports&lt;br /&gt;
&lt;br /&gt;
class OLMapFragment extends React.Component {&lt;br /&gt;
    constructor(props) {&lt;br /&gt;
        super(props)&lt;br /&gt;
        this.updateDimensions = this.updateDimensions.bind(this)&lt;br /&gt;
    }&lt;br /&gt;
    updateDimensions(){&lt;br /&gt;
        const h = window.innerWidth &amp;gt;= 992 ? window.innerHeight : 400&lt;br /&gt;
        this.setState({height: h})&lt;br /&gt;
    }&lt;br /&gt;
    componentWillMount(){&lt;br /&gt;
        window.addEventListener('resize', this.updateDimensions)&lt;br /&gt;
        this.updateDimensions()&lt;br /&gt;
    }&lt;br /&gt;
    componentDidMount(){&lt;br /&gt;
&lt;br /&gt;
        // Create an Openlayer Map instance with two tile layers&lt;br /&gt;
        const map = new Map({&lt;br /&gt;
            //  Display the map in the div with the id of map&lt;br /&gt;
            target: 'map',&lt;br /&gt;
            layers: [&lt;br /&gt;
                new TileLayer({&lt;br /&gt;
                    source: new XYZSource({&lt;br /&gt;
                        url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png',&lt;br /&gt;
                        projection: 'EPSG:3857'&lt;br /&gt;
                    })&lt;br /&gt;
                }),&lt;br /&gt;
                new TileLayer({&lt;br /&gt;
                    source: new TileWMSSource({&lt;br /&gt;
                        url: 'https://ahocevar.com/geoserver/wms',&lt;br /&gt;
                        params: {&lt;br /&gt;
                            layers: 'topp:states',&lt;br /&gt;
                            'TILED': true,&lt;br /&gt;
                        },&lt;br /&gt;
                        projection: 'EPSG:4326'&lt;br /&gt;
                    }),&lt;br /&gt;
                    name: 'USA'&lt;br /&gt;
                }),&lt;br /&gt;
            ],&lt;br /&gt;
            // Add in the following map controls&lt;br /&gt;
            controls: DefaultControls().extend([&lt;br /&gt;
                new ZoomSlider(),&lt;br /&gt;
                new MousePosition(),&lt;br /&gt;
                new ScaleLine(),&lt;br /&gt;
                new OverviewMap()&lt;br /&gt;
            ]),&lt;br /&gt;
            // Render the tile layers in a map view with a Mercator projection&lt;br /&gt;
            view: new View({&lt;br /&gt;
                projection: 'EPSG:3857',&lt;br /&gt;
                center: [0, 0],&lt;br /&gt;
                zoom: 2&lt;br /&gt;
            })&lt;br /&gt;
        })&lt;br /&gt;
    }&lt;br /&gt;
    componentWillUnmount(){&lt;br /&gt;
        window.removeEventListener('resize', this.updateDimensions)&lt;br /&gt;
    }&lt;br /&gt;
    render(){&lt;br /&gt;
        const style = {&lt;br /&gt;
            width: '100%',&lt;br /&gt;
            height:this.state.height,&lt;br /&gt;
            backgroundColor: '#cccccc',&lt;br /&gt;
        }&lt;br /&gt;
        return (&lt;br /&gt;
            &amp;lt;Grid container&amp;gt;&lt;br /&gt;
                &amp;lt;Grid item xs={12}&amp;gt;&lt;br /&gt;
                    &amp;lt;div id='map' style={style} &amp;gt;&lt;br /&gt;
                    &amp;lt;/div&amp;gt;&lt;br /&gt;
                &amp;lt;/Grid&amp;gt;&lt;br /&gt;
            &amp;lt;/Grid&amp;gt;&lt;br /&gt;
        )&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
export default OLMapFragment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
El script principal de React no és ''App.js'', sinó ''index.js'' (que normalment crida a ''App.js''). Per tant, anem a modifificar-lo:&lt;br /&gt;
&lt;br /&gt;
script ''src/index.js'':&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import React from 'react'&lt;br /&gt;
import { render} from 'react-dom'&lt;br /&gt;
import OLMapFragment from './js/components/OLMapFragment'&lt;br /&gt;
&lt;br /&gt;
render(&lt;br /&gt;
    &amp;lt;OLMapFragment /&amp;gt;&lt;br /&gt;
    ,&lt;br /&gt;
    document.getElementById('react-container')&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
veiem com s'importa el component '''OLMapFragment''',  i queda clar la ruta que ha de tenir ''OLMapFragment.js''.&lt;br /&gt;
&lt;br /&gt;
I finalment falta ''index.html'', que per tot el que hem dit ha d'estar a ''public/index.html''.&lt;br /&gt;
&lt;br /&gt;
script '''public/index.html''':&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html&amp;gt;&lt;br /&gt;
&amp;lt;html class=&amp;quot;no-js&amp;quot; lang=&amp;quot;en&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
    &amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;meta http-equiv=&amp;quot;x-ua-compatible&amp;quot; content='ie=edge'&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;Openlayers React&amp;lt;/title&amp;gt;&lt;br /&gt;
    &amp;lt;meta name=&amp;quot;description&amp;quot; content=&amp;quot;Explore planet Mars&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;!-- material-ui prerequisites --&amp;gt;&lt;br /&gt;
    &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&amp;amp;display=swap&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;https://fonts.googleapis.com/icon?family=Material+Icons&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;!-- end material-ui prerequisites --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css&amp;quot;&lt;br /&gt;
        type=&amp;quot;text/css&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;!--[if lte IE 8]&lt;br /&gt;
        &amp;lt;p class=&amp;quot;chromeframe&amp;quot;&amp;gt;You are using an &amp;lt;strong&amp;gt;outdated&amp;lt;/strong&amp;gt; browser. Please &amp;lt;a href=&amp;quot;http://browsehappy.com&amp;quot;&amp;gt;upgrade your browser&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
        &amp;lt;![endif]--&amp;gt;&lt;br /&gt;
    &amp;lt;div id='react-container'&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Aquí és on tenim el contenidor principal del mapa, i on tenim els links dels estils i fonts.&lt;br /&gt;
&lt;br /&gt;
Now, run the ReactJS project with a web server and open the web page with an Internet browser.&lt;br /&gt;
&lt;br /&gt;
Per tant, amb tot el que hem dit:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm start&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*http://localhost:3000&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ npm run build&lt;br /&gt;
$ serve -s build&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*http://localhost:5000&lt;br /&gt;
&lt;br /&gt;
i a través del Express:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ node server.js&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*localhost:8080&lt;br /&gt;
&lt;br /&gt;
{{Autor}}, abril 2020&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>