简体   繁体   English

如何调试“ Cannot POST /”错误?

[英]How to debug “Cannot POST /” error?

I am getting this error: 我收到此错误:

Cannot POST/. 无法发布/。

Below is the code I'm trying to execute. 以下是我要执行的代码。

Server.js 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 + '!');
});
});

This is my app.js file 这是我的app.js文件

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);
});

});

I've been trying this but not getting through.Not sure whether the html file I have is correct. 我一直在尝试但没有通过。不确定我拥有的html文件是否正确。 Want to create an html file to post from, but mine is refusing to respond. 想要创建一个要发布的html文件,但是我拒绝回复。

您无法POST/因为您尚未为到/ POST请求定义路由处理程序(对于GET/只有一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM