2016-07-06 2 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 하나를) 할 수 있습니다.

+0

당신이 말하는 것에 대해 확실하지 않습니다. Postman을 사용하여 게시, 가져 오기, 붙여 넣기 및 삭제를 수행하지만 브라우저에서이 작업을 수행하려고합니다. 코드에 대한 예를 들어 설명해 주시겠습니까? –

+0

@Israel : 우리는 사람들이 축 어적으로 입력 할 수있는 코드 조각을 필히 마친 것이 아니라 출발점으로 대답하도록 권장합니다. 나는 당신이'app.post ('/', function (req, res) {});'를 시도 할 수 있다고 상상해 봅니다. 물론 컨트롤러 함수에 무엇이 들어가는 지 결정해야합니다. – halfer

관련 문제