如何调试“无法POST /”错误?

时间:2016-07-06 13:05:46

标签: javascript node.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 + '!');
});
});

这是我的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);
});

});

我一直在尝试这个但没有通过。不确定我的html文件是否正确。想要创建一个html文件来发布,但我的拒绝回复。

1 个答案:

答案 0 :(得分:2)

您不能toLocalIteratorPOST,因为您尚未为/ POST的{​​{1}}请求定义路由处理程序(您只有一个/来自GET)。