実現したいこと
fetch
を使用してサーバサイドのエンドポイント/getMelody
から,melody.txt
の内容を非同期で取得したいと考えております.
fetch
を使用してサーバサイドのエンドポイント/docs
から,melody.txt
の内容を非同期で取得したいと考えております.
サーバサイドのコードをserver.js
に記述しています.
ひとまずはページ上のRun
ボタンを押下した際に,この取得を行い,デベロッパツールのコンソールにmelody.txt
の内容を出力しようと考えております.
https://github.com/snym-yt/VisualPMusic
このリポジトリでファイル構成,およびファイルの内容が確認することが可能です.
(質問本文での情報過多を防ぐためにこのような形にさせていただいております.ご了承ください)
index.html
, script.js
, server.js
, docs/melody.txt
の4ファイルが本質問に関係していると考えております.
環境
- MacBool Air
- Google Chrome
現状
Console
1:8080/getMelody:1 Failed to load resource: the server responded with a status of 404 (Not Found) 2script.js:121 Melody:
bash
1VisualPMusic % http-server 2 3Starting up http-server, serving ./ 4 5http-server version: 14.1.1 6 7http-server settings: 8CORS: disabled 9Cache: 3600 seconds 10Connection Timeout: 120 seconds 11Directory Listings: visible 12AutoIndex: visible 13Serve GZIP Files: false 14Serve Brotli Files: false 15Default File Extension: none 16 17Available on: 18 http://127.0.0.1:8080 19 http://192.168.0.16:8080 20Hit CTRL-C to stop the server
このようなエラー,状態が出ております.
確認したこと/お聞きしたいこと
- パスの確認
index.html
から見ても,server.js
から見ても,docs/melody.txt
の相対パスは問題ないと考えております.
- エンドポイントの確認
Express
を使用して齟齬なく設定されていると考えております.
- ローカルサーバの再起動
- キャッシュの削除
他にどのようなことを確認すればよろしいでしょうか,
また他に効率的なファイルの読み取り方法はあったりしますでしょうか.
追記1
"/getMelody"のとき
"/docs"のとき
追記2
express install 前
bash
1VisualPMusic % npm --version 210.1.0 3VisualPMusic % express --version 4zsh: command not found: express 5VisualPMusic % npm ls | grep express 6VisualPMusic % npm ls -g | grep express 7VisualPMusic % node server.js 8node:internal/modules/cjs/loader:1051 9 throw err; 10 ^ 11 12Error: Cannot find module 'express' 13Require stack: 14- VisualPMusic/server.js 15 at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15) 16 at Module._load (node:internal/modules/cjs/loader:901:27) 17 at Module.require (node:internal/modules/cjs/loader:1115:19) 18 at require (node:internal/modules/helpers:130:18) 19 at Object.<anonymous> (VisualPMusic/server.js:1:17) 20 at Module._compile (node:internal/modules/cjs/loader:1241:14) 21 at Module._extensions..js (node:internal/modules/cjs/loader:1295:10) 22 at Module.load (node:internal/modules/cjs/loader:1091:32) 23 at Module._load (node:internal/modules/cjs/loader:938:12) 24 at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) { 25 code: 'MODULE_NOT_FOUND', 26 requireStack: [ 'VisualPMusic/server.js' ] 27} 28 29Node.js v20.8.0
nodeのバージョン確認
bash
1VisualPMusic % Node --version 2v20.8.0
npm install express 結果
bash
1VisualPMusic % npm install express 2 3added 62 packages in 996ms 4 511 packages are looking for funding 6 run `npm fund` for details
npm init 結果
bash
1VisualPMusic % npm init 2This utility will walk you through creating a package.json file. 3It only covers the most common items, and tries to guess sensible defaults. 4 5See `npm help init` for definitive documentation on these fields 6and exactly what they do. 7 8Use `npm install <pkg>` afterwards to install a package and 9save it as a dependency in the package.json file. 10 11Press ^C at any time to quit. 12package name: (visualpmusic) 13version: (1.0.0) 14entry point: (script.js) 15git repository: 16keywords: 17author: 18license: (ISC) 19About to write to /VisualPMusic/package.json: 20 21{ 22 "dependencies": { 23 "express": "^4.18.2" 24 }, 25 "scripts": { 26 "start": "node server.js" 27 }, 28 "name": "visualpmusic", 29 "description": "## Purpose PMusicをビジュアルプログラミングにする.", 30 "version": "1.0.0", 31 "main": "script.js", 32 "directories": { 33 "doc": "docs" 34 }, 35 "devDependencies": {}, 36 "author": "", 37 "license": "ISC" 38} 39 40 41Is this OK? (yes) 42VisualPMusic % express --version 434.16.1
express install 後
bash
1VisualPMusic % express --version 24.16.1 3VisualPMusic % npm ls | grep express 4└── express@4.18.2 5VisualPMusic % npm ls -g | grep express 6├── express-generator@4.16.1 7VisualPMusic % node server.js 8Server is running on http://localhost:8080
https://kinsta.com/jp/knowledgebase/install-express/
この記事を見て,npm install -g express-generator
を行ったので-g
のものも反応してしまっているかもしれません
これは不都合でしょうか?
node server.js
を行った後http-server
を実行しましたが,エラーは同じものが出力されました.
/getMelody
を/docs/melody.txt
に変更したところ,404エラーはなくなりました.
しかし,melody.txt
の中身の出力を期待しているのに対して,異なったものが出力されてしまいました.
getMelodyを変更した後のコンソール(冒頭のみ抜粋)
Melody: <!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Index of /docs/</title> <style type="text/css">i.icon { display: block; height: 16px; width: 16px; } table tr { white-space: nowrap; } td.perms {} td.file-size { text-align: right; padding-left: 1em; } td.display-name { padding-left: 1em; } i.icon-_blank { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAWBJREFUeNqEUj1LxEAQnd1MVA4lyIEWx6UIKEGUExGsbC3tLfwJ/hT/g7VlCnubqxXBwg/Q4hQP/LhKL5nZuBsvuGfW5MGyuzM7jzdvVuR5DgYnZ+f99ai7Vt5t9K9unu4HLweI3qWYxI6PDosdy0fhcntxO44CcOBzPA7mfEyuHwf7ntQk4jcnywOxIlfxOCNYaLVgb6cXbkTdhJXq2SIlNMC0xIqhHczDbi8OVzpLSUa0WebRfmigLHqj1EcPZnwf7gbDIrYVRyEinurj6jTBHyI7pqVrFQqEbt6TEmZ9v1NRAJNC1xTYxIQh/MmRUlmFQE3qWOW1nqB2TWk1/3tgJV0waVvkFIEeZbHq4ElyKzAmEXOx6gnEVJuWBzmkRJBRPYGZBDsVaOlpSgVJE2yVaAe/0kx/3azBRO0VsbMFZE3CDSZKweZfYIVg+DZ6v7h9GDVOwZPw/PoxKu/fAgwALbDAXf7DdQkAAAAASUVORK5CYII="); } i.icon-_page {
txt:melody.txt
1play(60, 0.500000)

回答1件
あなたの回答
tips
プレビュー