2016-07-06 11 views
-1

私はこのエラーを取得しています:"POSTできません"というエラーをデバッグするには?

以下

Cannot POST/.

は、私が実行しようとしているコードです。

Server.js

var express = require('express'); 
var bodyParser = require('body-parser'); 
var _ = require('underscore'); 
var db = require('./db.js'); 
var bcryptjs = require('bcryptjs'); 
var middleware = require('./middleware.js')(db); 
var http = require('http').Server(app); 
var app = express(); 
var PORT = process.env.PORT || 3000; 
var todos = []; 
var todoNextId = 1; 
app.use(express.static(__dirname + '/public')); 

app.use(bodyParser.json()); 

app.get('/', function(req, res) { 
res.send('Todo API Root'); 
}); 

app.get('/todos', middleware.requireAuthentication, function(req, res) { 
var query = req.query; 
var where = { 
    userId: req.user.get('id') 
}; 

if (query.hasOwnProperty('completed') && query.completed === 'true') { 
    where.completed = true; 
} else if (query.hasOwnProperty('completed') && query.completed === 'false')  { 
    where.completed = false; 
} 

if (query.hasOwnProperty('q') && query.q.length > 0) { 
    where.description = { 
     $like: '%' + query.q + '%' 
    }; 
} 

db.todo.findAll({ 
    where: where 
}).then(function(todos) { 
    res.json(todos); 
}, function(e) { 
    res.status(500).send(); 
}); 

}); 

app.get('/todos/:id', middleware.requireAuthentication, function(req, res) { 
var todoId = parseInt(req.params.id, 10); 

db.todo.findOne({ 
    where: { 
     id: todoId, 
     userId: req.user.get('id') 
    } 
}).then(function(todo) { 
    if (!!todo) { 
     res.json(todo.toJSON()); 
    } else { 
     res.status(404).send(); 
    } 
}, function(e) { 
    res.status(500).send(); 
}); 

}); 

app.post('/todos', middleware.requireAuthentication, function(req, res) { 
var body = _.pick(req.body, 'description', 'completed'); 


db.todo.create(body).then(function(todo) { 
    req.user.addTodo(todo).then(function() { 
     return todo.reload(); 
    }).then(function (todo) { 
     res.json(todo.toJSON()); 
    }); 
}, function(e) { 
    res.status(400).json(e); 
}); 

}); 

app.delete('/todos/:id', middleware.requireAuthentication, function(req, res) { 
var todoId = parseInt(req.params.id, 10); 

db.todo.destroy({ 
    where: { 
     id: todoId, 
     userId: req.user.get('id') 
    } 
}).then(function(rowsDeleted) { 
    if (rowsDeleted === 0) { 
     res.send(404).json({ 
      error: 'No todo with id' 
     }); 
    } else { 
     res.status(204).send(); 
    } 
}, function() { 
    res.status(500).send(); 
}); 
}); 

app.put('/todos/:id', middleware.requireAuthentication, function(req, res) { 
var todoId = parseInt(req.params.id, 10); 
var body = _.pick(req.body, 'description', 'completed'); 
var attributes = {}; 

if (body.hasOwnProperty('completed')) { 
    attributes.completed = body.completed; 
} 

if (body.hasOwnProperty('description')) { 
    attributes.description = body.description; 
} 

db.todo.findOne({ 
    where: { 
     id: todoId, 
     userId: req.user.get('id') 
    } 
}).then(function(todo) { 
    if (todo) { 
     todo.update(attributes).then(function(todo) { 
      res.json(todo.toJSON()); 
     }, function(e) { 
      res.status(400).json(e); 
     }); 
    } else { 
     res.status(404).send(); 
    } 
}, function() { 
    res.status(500).send(); 
}); 
}); 


app.post('/users', function(req, res) { 
var body = _.pick(req.body, 'email', 'password'); 
db.user.create(body).then(function(user) { 
res.json(user.toPublicJSON()); 
}, function(e) { 
    res.status(400).json(e); 
}); 

}); 

app.post('/users/login', function (req, res) { 
var body = _.pick(req.body, 'email', 'password'); 
var userInstance; 

db.user.authenticate(body).then(function (user) { 
    var token = user.generateToken('authentication'); 
    userInstance = user; 

    return db.token.create({ 
     token: token 
    }); 
}).then(function (tokenInstance) { 
    res.header('Auth', 
tokenInstance.get('token')).json(userInstance.toPublicJSON()); 
}).catch(function() { 
    res.status(401).send(); 
}); 
});  

app.delete('/users/login', middleware.requireAuthentication, 
function (req, res) { 
req.token.destroy().then(function() { 
res.status(204).send(); 
}).catch(function() { 
res.status(500).send(); 
}); 
}); 

db.sequelize.sync({force: true}).then(function() { 
app.listen(PORT, function() { 
    console.log('Express listening on port ' + PORT + '!'); 
}); 
}); 

この私のapp.jsは、私はこれをしようが、私は持っているhtmlファイルが正しいかどうかを確認してくださいthrough.Not得ていないされてきた

app.post('/users', function(req, res) { 
var body = _.pick(req.body, 'email', 'password'); 


db.user.create(body).then(function(user) { 
    res.json(user.toPublicJSON()); 
}, function(e) { 
    res.status(400).json(e); 
}); 

}); 

ファイルです。投稿するHTMLファイルを作成したいのですが、私の返信は拒否しています。

+0

コンソールにはどのようなエラーが表示されていますか? –

+0

私はコンソールでエラーが発生していない、私は同じを行うために郵便配達を使用しており、それはすべてうまく動作します。フロントエンドルーティングのための方法が必要です。 –

答えて

1

あなたはないPOSTあなたが/POST要求のためのルートハンドラを定義していないので、/に(あなただけの/GETのための1つを持っている)ことができます。

+0

あなたが話していることについては不明です。ポストマンを使って投稿、取得、配置、削除を行っていますが、ブラウザからやりたいと思っています。あなたは私にコードの例を与えて、あなたの考えを私に提供してください。 –

+0

@イスラエル:私たちは、人々が逐語で入力できるコードを必ずしも完成させるのではなく、出発点として回答を得ることを奨励します。私はあなたが 'app.post( '/'、function(req、res){});'を試すことができると思います。もちろん、コントローラの機能に何が入るかを決める必要があります。 – halfer

関連する問題