Skip to content

Commit 160d248

Browse files
authored
Merge pull request #879 from NalinDalal/fix-on-2.0
solves docs issue 837 for p5.js 2.x
2 parents 03ab28c + 030bce6 commit 160d248

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/content/tutorials/en/getting-started-with-nodejs.mdx.todo renamed to src/content/tutorials/en/getting-started-with-nodejs.mdx

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ featuredImage: ../images/featured/node.png
77
featuredImageAlt: A p5.js logo with musical notes above it has arrows labeled with HTTP methods pointing to a cloud labeled “Internet”. Above the cloud is an icon that reads “http\://”. Arrows point from the cloud to a pink cube labeled “Web Server” with the Node.js logo above it.
88
relatedContent:
99
references:
10-
- en/p5/preload
10+
- en/p5/async_await
1111
- en/p5/loadjson
1212
authors:
1313
- Layla Quiñones
1414
- Nick McIntyre
1515
---
1616

17-
In this guide, we'll explore the fusion of p5.js and [Node.js](http://node.js) to create dynamic applications that save and retrieve user-generated drawings, animations, and sound projects! For example, you can create a [Simple Melody App](/tutorials/simple-melody-app/) where you save files with melodies you create by interacting with the canvas! Node.js allows you to easily save, replay, and edit these files right from your browser!
17+
In this guide, we'll explore the fusion of p5.js and [Node.js](http://nodejs.org) to create dynamic applications that save and retrieve user-generated drawings, animations, and sound projects! For example, you can create a [Simple Melody App](/tutorials/simple-melody-app/) where you save files with melodies you create by interacting with the canvas! Node.js allows you to easily save, replay, and edit these files right from your browser!
1818

1919

2020
![A p5.js logo with musical notes above it has arrows labeled with HTTP methods pointing to a cloud labeled “Internet”. Above the cloud is an icon that reads “http://”. Arrows point from the cloud to a pink cube labeled “Web Server” with the Node.js logo above it.](../images/advanced/node.png)
@@ -175,37 +175,31 @@ let fs = require('fs');
175175

176176
// API endpoint to get list of songs
177177
app.get('/songs', (req, res) => {
178-
  fs.readdir('songs', (err, files) => {
179-
    if (err) {
180-
      res.status(500).send('Error reading song files');
181-
    } else {
182-
      res.json({ files });
183-
    }
184-
  });
178+
fs.readdir('songs', (err, files) => {
179+
if (err) {
180+
res.status(500).send('Error reading song files');
181+
} else {
182+
res.json({ files });
183+
}
184+
});
185185
});
186186
```
187187

188188
The code above initializes the [file system module (`fs`)](https://nodejs.org/api/fs.html), which provides APIs that help users interact with file systems on their computers. It also uses the `app.get()` method, which handles GET HTTP requests to the `/songs` URL. Here you use the file system module to read filenames from the folder, parse them as a JSON object, and send them in the response to the GET request.
189189

190190
Now that you have instructed the GET request on how to read the songs folder filenames, we can upload filenames as a JSON object in `sketch.js`.
191191

192-
- Use `preload()` and `loadJSON()` to load files from the songs folder in a global variable named `songs`. Visit the [p5.js reference](/reference) to learn more about [`preload()`](/reference/p5/preload) and [`loadJSON()`](/reference/p5/loadJSON).
193-
- Use `console.log(songs)` in `setup()` to print the contents of the JSON array.
192+
**Note:** In p5.js 2.x, the `preload()` function and synchronous loading methods have been removed. Instead, you should use `async`/`await` and the browser's `fetch` API to load data before using it in your sketch. Here is how you can update your `sketch.js`:
194193

195-
Your sketch.js file should look like this:
196194

197195
```js
198-
//variable for JSON object with file names
199196
let songs;
200197

201-
function preload() {
202-
  //load and save the songs folder as JSON
203-
  songs = loadJSON("/songs");
204-
}
205-
206-
function setup() {
207-
  createCanvas(400, 400);
208-
  console.log(songs)
198+
async function setup() {
199+
createCanvas(400, 400);
200+
// Fetch the list of songs from the server via loadJSON
201+
songs = await loadJSON('/songs');
202+
console.log(songs);
209203
}
210204

211205
function draw() {
@@ -216,13 +210,11 @@ function draw() {
216210
View your browser’s console to make sure the output of the `songs` variable looks something like this:
217211

218212
```
219-
Object i
220-
  files: Array(3)
221-
    0: "C Major Scale.json"
222-
    1: "Frere Jacques.json"
223-
    2: "Mary's Lamb.json"
224-
    length: 3
225-
// ...prototype
213+
{ files: [
214+
"C Major Scale.json",
215+
"Frere Jacques.json",
216+
"Mary's Lamb.json"
217+
] }
226218
```
227219

228220
Now you are ready to build the [Melody App](https://docs.google.com/document/u/0/d/1mzJv-7qU1_CmkWI0ZFeqf3CeBfpOOVIrvPRZtxqFxRI/edit)! You can access completed code for this guide in [this Github repository](https://github.com/MsQCompSci/melodyAppNodeStarter/tree/main).

0 commit comments

Comments
 (0)