错误App.post Nodejs

时间:2015-10-27 20:15:10

标签: javascript node.js sqlite

我是nodejs的菜鸟,我正在使用它和sqlite3做一个数据库,它是一个基本的http服务器,我不认为问题存在,但当我制作我创建的模型的代码数据库我有这个错误:

app.post is not a function
TypeError: app.post is not a function
    at module.exports (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\v\routes\index.js:41:9)
    at Layer.handle [as handle_request] (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\lib\router\index.js:271:10)
    at SendStream.error (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\node_modules\serve-static\index.js:120:7)
    at emitOne (events.js:77:13)
    at SendStream.emit (events.js:169:7)
    at SendStream.error (C:\Users\anmocar1\Desktop\node\NodeJSPortable\Data\node_modules\express\node_modules\send\index.js:245:17)

这是文件Index.js的代码:

var UserModel = require('../models/user');
module.exports = function(app)
{
    app.get("/createTable", function(req, res)
    {
        UserModel.createUsersTable();
        res.end();
    });
    app.get("/", function(req, res)
    {
        res.render('index', { 
            titulo: 'Formularios en NodeJS con Bootstrap'
        });
    });
    app.get("/users", function(req, res){
        UserModel.getUsers(function(error, data)
        {
            res.render('users', { 
                titulo: 'Usuarios registrados en el blog',
                usuarios : data
            });
        });
    });
    app.post("/register", function(req,res){
        UserModel.registerUser({username:req.body.username,password:req.body.password}, function(data)
        {
            if(data)
            {
                if(data.msg === "existe")
                {
                    res.send("existe", 200);
                }
                else
                {
                    res.send("creado", 200);
                }
            }
            else
            {
                res.send("error", 400);
            }
        });
        res.end();
    });
}

我已经检查过了,方法写的方法是对的,我不知道会出现什么问题,如果您有任何想法,请告诉我,谢谢

1 个答案:

答案 0 :(得分:0)

解决此问题的更好方法是调用

module.exports = app;

在代码的最后一行。

所以它会像;

app.get(//your code);
app.post(//your code);
//etc..

module.exports = app;