質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

1回答

229閲覧

node.jsのexpressでAPIサーバを作りたい

lightfull

総合スコア11

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

1グッド

0クリップ

投稿2019/01/06 16:08

編集2019/01/07 02:24

Qiitaの記事を書いた人に質問すれば良いのかもしれませんが、記事が書かれてから時間が経っているので、ここで質問させてください。

Expressを用いた簡単なAPIの作り方
https://qiita.com/yujiro0102/items/a973a0ae05a794d24eb5

上記サイトを見ながら、全く同じものを作ってみたのですが、
ブラウザで
http://localhost:3000/get-example
を表示すると、以下のエラーが表示されます。

TypeError: Cannot read property 'json' of undefined at Object.exports.get_example (/Users/atom/dev/sample/express-node-example/express-exports.js:13:6) at /Users/atom/dev/sample/express-node-example/express-example.js:19:15 at Layer.handle [as handle_request] (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/layer.js:95:5) at next (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/layer.js:95:5) at /Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/index.js:335:12) at next (/Users/atom/dev/sample/express-node-example/node_modules/express/lib/router/index.js:275:10) at jsonParser (/Users/atom/dev/sample/express-node-example/node_modules/body-parser/lib/types/json.js:110:7)

記事の中でnodeの起動が、

node stripe-express.js

と書かれているのは、

node express-example.js

の誤りかと思って、起動しています。

実施した手順(サイトの記載のまま)

ここではexpress-node-exampleとします。 $ mkdir express-node-example ディレクトリへ移動しましょう。 $ cd express-node-example node.jsを用いるためにnpmをinitします。 $ npm init Expressをインストールします。 $ npm install express --save

ソース(サイトのコードを丸コピー)

express-example.js

//expressを使用するのでその設定 const express = require('express'); const bodyParser = require('body-parser'); const app = express(); //処理を記述した外部ファイルを参照 var export_func = require("./express-exports"); // urlencodedとjsonは別々に初期化 app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); //listenします。カッコ内の数字を変更することで解放するポート番号を変更できます。 app.listen(3000); //確認のためコンソールに出力します。 console.log('Server is online.'); //app.getでGETすることができます。最初の引数を変更することでURLが変更できます。二つ目が実行内容です。 app.get('/get-example', function(req, res) { export_func.get_example(); }) //app.getでPOSTすることができます。最初の引数を変更することでURLが変更できます。二つ目が実行内容です。 app.post('/post-example', function(req, res) { export_func.post_example(req.id, req.name); })

package-lock.jsonは、長すぎてはれませんでした。

saka_moto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dice142

2019/01/07 02:11

「全く同じもの」とありますが、誤字脱字があるなどのミスが原因であることが多いです。 参考元の記事ではなく質問者様が書かれたコードがないとこちらは判断できないのでコードの提示をお願いします。
guest

回答1

0

ベストアンサー

参考にされている記事に不備があるようですね。
(指摘する前に参考元の記事を確認しておくべきでした。すみません。)

記事の中でnodeの起動が、

node stripe-express.js
と書かれているのは、
node express-example.js
の誤りかと思って、起動しています。

記事の誤りのようですので質問者様の起動方法で問題ないです。


問題の部分ですが、express-example.jsで下記の呼び出しを行っています。

JavaScript

1app.get('/get-example', function(req, res) { 2 export_func.get_example(); 3})

で、express-exports.jsで関数の実装がされています。

JavaScript

1exports.get_example=function(res, req, next){ 2 res.json(json_example); 3}

関数の引数にresreqnextがありますが、呼び出しでは引数が設定されていません。
元記事のコメントで指摘されているので、取り急ぎ確認せず修正したというところでしょうか。

呼び出しのところ(express-example.js)を以下のように直すと実行できます。

JavaScript

1app.get('/get-example', function(req, res) { 2 export_func.get_example(res); 3})

投稿2019/01/07 03:05

dice142

総合スコア5158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

lightfull

2019/01/07 03:11

無事に動きましたー!! 基本がわかってないので、全然想像つかなくて困っていました。 ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問