简体   繁体   English

错误的App.post Node.js

[英]Error App.post Nodejs

I'm a noob with nodejs and I'm doing a database with that and sqlite3, it's a basic http server, I don't think the problem be there, but when I make the code of the models where I create the database I have this error: 我是nodejs的新手,并且正在使用sqlite3和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)

this is the code of the file Index.js: 这是文件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();
    });
}

I've checked it and the way is written the method is right, I don't know what could be the problem, if you have any idea, please tell me, thank you 我检查了一下,写的方法正确,我不知道可能是什么问题,如果您有任何想法,请告诉我,谢谢

A better approach to this problem will be to call the 解决此问题的更好方法是将

module.exports = app;

at the last line of the code. 在代码的最后一行。

So it will be like; 这样就好了;

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

module.exports = app; module.exports =应用程序;

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

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