Skip to content

Commit 1013288

Browse files
committed
vite parts 2-3
1 parent f0e4473 commit 1013288

File tree

10 files changed

+126
-111
lines changed

10 files changed

+126
-111
lines changed

src/content/0/en/part0a.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Despite changes <i>all the submitted exercises remain valid</i>, and the course
340340
341341
Recent major changes
342342
343-
- Parts 1 and 2 (11-14th August): Create React app replaced with Vite
343+
- Parts 1-3 (11-14th August): Create React app replaced with Vite
344344
345345
### Expanding on a previously completed course
346346

src/content/0/fi/osa0a.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Kurssilla ei ole enää vuosittaisia versiota. Kurssi on siis käynnissä koko a
282282
Muutoksista huolimatta <i>kaikki jo tehdyt palautukset säilyvät voimassa</i>, eli voit jatkaa kurssia päivityksistä huolimatta normaaliin tapaan.
283283

284284
Viimeaikaisia isompia muutoksia
285-
- Osat 1 ja 2 (11-14.8.2023): Create React app korvattu Vitellä
285+
- Osat 1-3 (11-14.8.2023): Create React app korvattu Vitellä
286286

287287
### Aiemmin suoritetun kurssin täydentäminen
288288

src/content/1/fi/osa1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ lang: fi
99
Alamme tässä osassa tutustua React-kirjastoon, jolla siis teemme sovelluksen selaimessa suoritettavan koodin. Teemme samalla myös nopean katsauksen Javascriptin Reactin kannalta oleellisimpiin ominaisuuksiin.
1010

1111
<i>Osa päivitetty 11.8.2023</i>
12-
- <i>Create React App rkorvattu Vitellä</i>
12+
- <i>Create React App korvattu Vitellä</i>
1313

1414
</div>

src/content/3/en/part3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: en
88

99
In this part our focus shifts towards the backend, that is, towards implementing functionality on the server side of the stack. We will implement a simple REST API in Node.js by using the Express library, and the application's data will be stored in a MongoDB database. At the end of this part, we will deploy our application to the internet.
1010

11-
<i>Part updated 19th Jan 2023</i>
12-
- <i>Added instructions for [https://render.com/](https://render.com/) hosting platform</i>
11+
<i>Part updated 14th August 2023</i>
12+
- <i>Create React App replaced with Vite</i>
1313

1414
</div>

src/content/3/en/part3b.md

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -146,38 +146,25 @@ Give the app a name or let Fly.io auto-generate one. Pick a region where the app
146146

147147
The last question is "Would you like to deploy now?". We should answer "no" since we are not quite ready yet.
148148

149-
Fly.io creates a file <i>fly.toml</i> in the root of your app where the app is configured. To get the app up and running we <i>might</i> need to do a small addition to the part [env] of the configuration:
149+
Fly.io creates a file <i>fly.toml</i> in the root of your app where the app is configured. To get the app up and running we <i>might</i> need to do a small addition to the configuration:
150150

151151
```bash
152-
[env]
153-
PORT = "8080" # add this
152+
[build]
154153

155-
[experimental]
156-
auto_rollback = true
154+
[env]
155+
PORT = "3000" # add this
157156

158-
[[services]]
159-
http_checks = []
160-
internal_port = 8080
157+
[http_service]
158+
internal_port = 3000 # ensure that this is same as PORT
159+
force_https = true
160+
auto_stop_machines = true
161+
auto_start_machines = true
162+
min_machines_running = 0
161163
processes = ["app"]
162164
```
163165

164166
We have now defined in the part [env] that environment variable PORT will get the correct port (defined in part [services]) where the app should create the server. Note that the definition might be already there, but some times it has been missing.
165167

166-
Note: For some users, the fly.toml file generated by fly.io will have this configuration:
167-
168-
```bash
169-
[http_service]
170-
internal_port = 3000
171-
```
172-
173-
This will cause the following warning once deployed and result in your app being inacessible:
174-
175-
```bash
176-
WARNING The app is not listening on the expected address and will not be reachable by fly-proxy
177-
```
178-
179-
Change internal_port to 8080 if your configuration is as mentioned above.
180-
181168
We are now ready to deploy the app to the Fly.io servers. That is done with the following command:
182169

183170
```bash
@@ -190,12 +177,6 @@ If all goes well, the app should now be up and running. You can open it in the b
190177
fly open
191178
```
192179

193-
After the initial setup, when the app code has been updated, it can be deployed to production with the command
194-
195-
```bash
196-
fly deploy
197-
```
198-
199180
A particularly important command is _fly logs_. This command can be used to view server logs. It is best to keep logs always visible!
200181

201182
**Note:** In some cases (the cause is so far unknown) running Fly.io commands especially on Windows WSL has caused problems. If the following command just hangs
@@ -218,6 +199,13 @@ $ flyctl ping -o personal
218199

219200
then there are no connection problems!
220201

202+
Whenever you make changes to the application, you can take the new version to production with a command
203+
204+
```bash
205+
fly deploy
206+
```
207+
208+
221209
#### Render
222210

223211
The following assumes that the [sign in](https://dashboard.render.com/) has been made with a GitHub account.
@@ -263,13 +251,13 @@ app.listen(PORT, () => {
263251

264252
So far we have been running React code in <i>development mode</i>. In development mode the application is configured to give clear error messages, immediately render code changes to the browser, and so on.
265253

266-
When the application is deployed, we must create a [production build](https://reactjs.org/docs/optimizing-performance.html#use-the-production-build) or a version of the application which is optimized for production.
254+
When the application is deployed, we must create a [production build](https://vitejs.dev/guide/build.html) or a version of the application which is optimized for production.
267255

268-
A production build of applications created with <i>create-react-app</i> can be created with the command [npm run build](https://github.com/facebookincubator/create-react-app#npm-run-build-or-yarn-build).
256+
A production build of applications created with Vite can be created with the command [npm run build](https://vitejs.dev/guide/build.html).
269257

270258
Let's run this command from the <i>root of the notes frontend project</i> that we developed in [Part 2](/en/part2).
271259

272-
This creates a directory called <i>build</i> (which contains the only HTML file of our application, <i>index.html</i> ) which contains the directory <i>static</i>. [Minified](<https://en.wikipedia.org/wiki/Minification_(programming)>) version of our application's JavaScript code will be generated in the <i>static</i> directory. Even though the application code is in multiple files, all of the JavaScript will be minified into one file. All of the code from all of the application's dependencies will also be minified into this single file.
260+
This creates a directory called <i>dist</i> (which contains the only HTML file of our application, <i>index.html</i> ) which contains the directory <i>assets</i>. [Minified](<https://en.wikipedia.org/wiki/Minification_(programming)>) version of our application's JavaScript code will be generated in the <i>dist</i> directory. Even though the application code is in multiple files, all of the JavaScript will be minified into one file. All of the code from all of the application's dependencies will also be minified into this single file.
273261

274262
The minified code is not very readable. The beginning of the code looks like this:
275263

@@ -284,24 +272,24 @@ One option for deploying the frontend is to copy the production build (the <i>bu
284272
We begin by copying the production build of the frontend to the root of the backend. With a Mac or Linux computer, the copying can be done from the frontend directory with the command
285273
286274
```bash
287-
cp -r build ../backend
275+
cp -r dist ../backend
288276
```
289277
290278
If you are using a Windows computer, you may use either [copy](https://www.windows-commandline.com/windows-copy-command-syntax-examples/) or [xcopy](https://www.windows-commandline.com/xcopy-command-syntax-examples/) command instead. Otherwise, simply copy and paste.
291279
292280
The backend directory should now look as follows:
293281
294-
![bash screenshot of ls showing build directory](../../images/3/27new.png)
282+
![bash screenshot of ls showing build directory](../../images/3/27v.png)
295283
296-
To make express show <i>static content</i>, the page <i>index.html</i> and the JavaScript, etc., it fetches, we need a built-in middleware from express called [static](http://expressjs.com/en/starter/static-files.html).
284+
To make express show <i>static content</i>, the page <i>index.html</i> and the JavaScript, etc., it fetches, we need a built-in middleware from Express called [static](http://expressjs.com/en/starter/static-files.html).
297285
298286
When we add the following amidst the declarations of middlewares
299287
300288
```js
301-
app.use(express.static('build'))
289+
app.use(express.static('dist'))
302290
```
303291
304-
whenever express gets an HTTP GET request it will first check if the <i>build</i> directory contains a file corresponding to the request's address. If a correct file is found, express will return it.
292+
whenever express gets an HTTP GET request it will first check if the <i>dist</i> directory contains a file corresponding to the request's address. If a correct file is found, express will return it.
305293
306294
Now HTTP GET requests to the address <i>www.serversaddress.com/index.html</i> or <i>www.serversaddress.com</i> will show the React frontend. GET requests to the address <i>www.serversaddress.com/api/notes</i> will be handled by the backend's code.
307295
@@ -327,20 +315,25 @@ The application can now be used from the <i>backend</i> address <http://localhos
327315
328316
Our application now works exactly like the [single-page app](/en/part0/fundamentals_of_web_apps#single-page-app) example application we studied in part 0.
329317
330-
When we use a browser to go to the address <http://localhost:3001>, the server returns the <i>index.html</i> file from the <i>build</i> repository. The summarized contents of the file are as follows:
318+
When we use a browser to go to the address <http://localhost:3001>, the server returns the <i>index.html</i> file from the <i>dist</i> directory. The contents of the file are as follows:
331319
332320
```html
333-
<head>
334-
<meta charset="utf-8"/>
335-
<title>React App</title>
336-
<link href="/static/css/main.f9a47af2.chunk.css" rel="stylesheet">
337-
</head>
338-
<body>
339-
<div id="root"></div>
340-
<script src="/static/js/1.578f4ea1.chunk.js"></script>
341-
<script src="/static/js/main.104ca08d.chunk.js"></script>
342-
</body>
321+
<!doctype html>
322+
<html lang="en">
323+
<head>
324+
<meta charset="UTF-8" />
325+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
326+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
327+
<title>Vite + React</title>
328+
<script type="module" crossorigin src="/assets/index-5f6faa37.js"></script>
329+
<link rel="stylesheet" href="/assets/index-198af077.css">
330+
</head>
331+
<body>
332+
<div id="root"></div>
333+
334+
</body>
343335
</html>
336+
344337
```
345338
346339
The file contains instructions to fetch a CSS stylesheet defining the styles of the application, and two <i>script</i> tags that instruct the browser to fetch the JavaScript code of the application - the actual React application.
@@ -452,7 +445,7 @@ Another option is the use of [shx](https://www.npmjs.com/package/shx).
452445
453446
### Proxy
454447
455-
Changes on the frontend have caused it to no longer work in development mode (when started with command _npm start_), as the connection to the backend does not work.
448+
Changes on the frontend have caused it to no longer work in development mode (when started with command _npm run dev_), as the connection to the backend does not work.
456449
457450
![Network dev tools showing a 404 on getting notes](../../images/3/32new.png)
458451
@@ -464,29 +457,36 @@ const baseUrl = '/api/notes'
464457
465458
Because in development mode the frontend is at the address <i>localhost:3000</i>, the requests to the backend go to the wrong address <i>localhost:3000/api/notes</i>. The backend is at <i>localhost:3001</i>.
466459
467-
If the project was created with create-react-app, this problem is easy to solve. It is enough to add the following declaration to the <i>package.json</i> file of the frontend repository.
460+
If the project was created with Vite, this problem is easy to solve. It is enough to add the following declaration to the <i>vite.config.json</i> file of the frontend repository.
468461
469462
```bash
470-
{
471-
"dependencies": {
472-
// ...
473-
},
474-
"scripts": {
475-
// ...
463+
import { defineConfig } from 'vite'
464+
import react from '@vitejs/plugin-react'
465+
466+
// https://vitejs.dev/config/
467+
export default defineConfig({
468+
plugins: [react()],
469+
// highlight-start
470+
server: {
471+
proxy: {
472+
'/api': {
473+
target: 'http://localhost:3001',
474+
changeOrigin: true,
475+
},
476+
}
476477
},
477-
"proxy": "http://localhost:3001" // highlight-line
478-
}
478+
// highlight-end
479+
})
480+
479481
```
480482
481-
After a restart, the React development environment will work as a [proxy](https://create-react-app.dev/docs/proxying-api-requests-in-development/). If the React code does an HTTP request to a server address at <i><http://localhost:3000></i> not managed by the React application itself (i.e. when requests are not about fetching the CSS or JavaScript of the application), the request will be redirected to the server at <i><http://localhost:3001></i>.
483+
After a restart, the React development environment will work as a [proxy](https://vitejs.dev/config/server-options.html#server-proxy). If the React code does an HTTP request to a server address at <i><http://localhost:3000></i> not managed by the React application itself (i.e. when requests are not about fetching the CSS or JavaScript of the application), the request will be redirected to the server at <i><http://localhost:3001></i>.
482484
483485
Now the frontend is also fine, working with the server both in development- and production mode.
484486
485-
A negative aspect of our approach is how complicated it is to deploy the frontend. Deploying a new version requires generating a new production build of the frontend and copying it to the backend repository. This makes creating an automated [deployment pipeline](https://martinfowler.com/bliki/DeploymentPipeline.html) more difficult. Deployment pipeline means an automated and controlled way to move the code from the computer of the developer through different tests and quality checks to the production environment. Building a deployment pipeline is the topic of [part 11](https://fullstackopen.com/en/part11) of this course.
486-
487-
There are multiple ways to achieve this (for example placing both backend and frontend code [in the same repository](https://github.com/mars/heroku-cra-node) ) but we will not go into those now.
487+
A negative aspect of our approach is how complicated it is to deploy the frontend. Deploying a new version requires generating a new production build of the frontend and copying it to the backend repository. This makes creating an automated [deployment pipeline](https://martinfowler.com/bliki/DeploymentPipeline.html) more difficult. Deployment pipeline means an automated and controlled way to move the code from the computer of the developer through different tests and quality checks to the production environment. Building a deployment pipeline is the topic of [part 11](https://fullstackopen.com/en/part11) of this course. There are multiple ways to achieve this (for example placing both backend and frontend code in the same repository but we will not go into those now.
488488
489-
In some situations, it may be sensible to deploy the frontend code as its own application. With apps created with create-react-app it is [straightforward](https://github.com/mars/create-react-app-buildpack).
489+
In some situations, it may be sensible to deploy the frontend code as its own application.
490490
491491
The current backend code can be found on [Github](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part3-3), in the branch <i>part3-3</i>. The changes in frontend code are in <i>part3-1</i> branch of the [frontend repository](https://github.com/fullstack-hy2020/part2-notes/tree/part3-1).
492492
@@ -524,7 +524,7 @@ Generate a production build of your frontend, and add it to the internet applica
524524
525525
**NB** If you use Render, make sure the directory <i>build</i> is not gitignored
526526
527-
Also, make sure that the frontend still works locally (in development mode when started with command _npm start_).
527+
Also, make sure that the frontend still works locally (in development mode when started with command _npm run dev_).
528528
529529
If you have problems getting the app working make sure that your directory structure matches [the example app](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part3-3).
530530

src/content/3/fi/osa3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: fi
88

99
Tässä osassa fokus siirtyy backendin, eli palvelimen toiminnallisuuden toteuttamiseen. Toteutamme Node.js:n Express-kirjastoa hyödyntäen yksinkertaisen REST-apin, joka tallettaa dataa MongoDB-tietokantaan. Viemme myös sovelluksemme internetiin.
1010

11-
<i>Osa päivitetty 19.1.2023</i>
12-
- <i>Ohje [https://render.com/](https://render.com/)-palvelun käyttöön lisätty</i>
11+
<i>Osa päivitetty 11.8.2023</i>
12+
- <i>Create React App korvattu Vitellä</i>
1313

1414
</div>

0 commit comments

Comments
 (0)