###前提・実現したいこと
http://qiita.com/shopetan/items/58a62a366aac4f5faa20
のとおりに進めているのですが、うまくPOSTできませんどういうことなのでしょうか?
いまのところコピペしているのでコードのエラーはないと思います。
エラーは吐き出してません。
###発生している問題・エラーメッセージ
ポストマン結果
Could not get any response There was an error connecting to http://localhost:3001/api/users. Why this might have happened: The server couldn't send a response: Ensure that the backend is working properly Self-signed SSL certificates are being blocked: Fix this by turning off 'SSL certificate verification' in Settings > General Client certificates are required for this server: Fix this by adding client certificates in Settings > Certificates Request timeout: Change request timeout in Settings > General
###該当のソースコード
node.js
1// server.js 2 3// 必要なパッケージの読み込み 4var express = require('express'); 5var app = express(); 6var bodyParser = require('body-parser'); 7 8// DBへの接続 9var mongoose = require('mongoose'); 10mongoose.connect('mongodb://localhost/jsonAPI'); 11 12// モデルの宣言 13var User = require('./app/models/user'); 14 15// POSTでdataを受け取るための記述 16app.use(bodyParser.urlencoded({ extended: true })); 17app.use(bodyParser.json()); 18 19// 3000番を指定 20var port = process.env.PORT || 3001; 21 22// expressでAPIサーバを使うための準備 23var router = express.Router(); 24 25router.use(function(req, res, next) { 26 console.log('Something is happening.'); 27 next(); 28}); 29 30// 正しく実行出来るか左記にアクセスしてテストする (GET http://localhost:3000/api) 31router.get('/', function(req, res) { 32 res.json({ message: 'Successfully Posted a test message.' }); 33}); 34 35// /users というルートを作成する. 36// ---------------------------------------------------- 37router.route('/users') 38 39// ユーザの作成 (POST http://localhost:3000/api/users) 40 .post(function(req, res) { 41 42 // 新しいユーザのモデルを作成する. 43 var user = new User(); 44 45 // ユーザの各カラムの情報を取得する. 46 user.twitter_id = req.body.twitter_id; 47 user.name = req.body.name; 48 user.age = req.body.age; 49 50 // ユーザ情報をセーブする. 51 user.save(function(err) { 52 if (err) 53 res.send(err); 54 res.json({ message: 'User created!' }); 55 }); 56 }) 57 58// 全てのユーザ一覧を取得 (GET http://localhost:8080/api/users) 59 .get(function(req, res) { 60 User.find(function(err, users) { 61 if (err) 62 res.send(err); 63 res.json(users); 64 }); 65 }); 66 67// ルーティング登録 68app.use('/api', router); 69 70//サーバ起動 71app.listen(port); 72console.log('listen on port ' + port); 73
###試したこと
POSTのURLで http://localhost:8080/api/users
とありますがこれが間違っていると思い
http://localhost:8080/users
http://localhost:3001/api/users
http://localhost:3001/users
など試したのですがエラーが起こりました
###補足情報(言語/FW/ツール等のバージョンなど)
postman使ってます
3001でローカル接続してます。
http://localhost:3001/api
でのGETは成功してます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。