You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/1/fi/osa1.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,6 @@ lang: fi
9
9
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.
Copy file name to clipboardExpand all lines: src/content/3/en/part3.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: en
8
8
9
9
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.
10
10
11
-
<i>Part updated 19th Jan 2023</i>
12
-
- <i>Added instructions for [https://render.com/](https://render.com/) hosting platform</i>
Copy file name to clipboardExpand all lines: src/content/3/en/part3b.md
+64-64Lines changed: 64 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -146,38 +146,25 @@ Give the app a name or let Fly.io auto-generate one. Pick a region where the app
146
146
147
147
The last question is "Would you like to deploy now?". We should answer "no" since we are not quite ready yet.
148
148
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:
150
150
151
151
```bash
152
-
[env]
153
-
PORT = "8080"# add this
152
+
[build]
154
153
155
-
[experimental]
156
-
auto_rollback = true
154
+
[env]
155
+
PORT = "3000"# add this
157
156
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
161
163
processes = ["app"]
162
164
```
163
165
164
166
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.
165
167
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
-
181
168
We are now ready to deploy the app to the Fly.io servers. That is done with the following command:
182
169
183
170
```bash
@@ -190,12 +177,6 @@ If all goes well, the app should now be up and running. You can open it in the b
190
177
fly open
191
178
```
192
179
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
-
199
180
A particularly important command is _fly logs_. This command can be used to view server logs. It is best to keep logs always visible!
200
181
201
182
**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
218
199
219
200
then there are no connection problems!
220
201
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
+
221
209
#### Render
222
210
223
211
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, () => {
263
251
264
252
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.
265
253
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.
267
255
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).
269
257
270
258
Let's run this command from the <i>root of the notes frontend project</i> that we developed in [Part 2](/en/part2).
271
259
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.
273
261
274
262
The minified code is not very readable. The beginning of the code looks like this:
275
263
@@ -284,24 +272,24 @@ One option for deploying the frontend is to copy the production build (the <i>bu
284
272
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
285
273
286
274
```bash
287
-
cp -r build ../backend
275
+
cp -r dist ../backend
288
276
```
289
277
290
278
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.
291
279
292
280
The backend directory should now look as follows:
293
281
294
-

282
+

295
283
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).
297
285
298
286
When we add the following amidst the declarations of middlewares
299
287
300
288
```js
301
-
app.use(express.static('build'))
289
+
app.use(express.static('dist'))
302
290
```
303
291
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.
305
293
306
294
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.
307
295
@@ -327,20 +315,25 @@ The application can now be used from the <i>backend</i> address <http://localhos
327
315
328
316
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.
329
317
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:
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).
452
445
453
446
### Proxy
454
447
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.
456
449
457
450

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>.
466
459
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.
468
461
469
462
```bash
470
-
{
471
-
"dependencies": {
472
-
// ...
473
-
},
474
-
"scripts": {
475
-
// ...
463
+
import { defineConfig } from'vite'
464
+
importreactfrom'@vitejs/plugin-react'
465
+
466
+
// https://vitejs.dev/config/
467
+
exportdefaultdefineConfig({
468
+
plugins: [react()],
469
+
// highlight-start
470
+
server: {
471
+
proxy: {
472
+
'/api': {
473
+
target:'http://localhost:3001',
474
+
changeOrigin:true,
475
+
},
476
+
}
476
477
},
477
-
"proxy":"http://localhost:3001"// highlight-line
478
-
}
478
+
// highlight-end
479
+
})
480
+
479
481
```
480
482
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>.
482
484
483
485
Now the frontend is also fine, working with the server both in development- and production mode.
484
486
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.
488
488
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.
490
490
491
491
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).
492
492
@@ -524,7 +524,7 @@ Generate a production build of your frontend, and add it to the internet applica
524
524
525
525
**NB** If you use Render, make sure the directory <i>build</i> is not gitignored
526
526
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_).
528
528
529
529
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).
Copy file name to clipboardExpand all lines: src/content/3/fi/osa3.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ lang: fi
8
8
9
9
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.
10
10
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>
0 commit comments