无法在node.js中发布表单

时间:2014-07-07 09:41:12

标签: javascript node.js express

我非常熟悉node.js。

有一个示例表单,我试图在数据库中插入值 -

这是我的测试页 -

<form action="/create" method="POST" class="form-horizontal"  enctype="application/x-www-form-urlencoded">
     <input type="text" id="username_input" name="username">
     <input type="text" id="password_input" name="password">
     <input type="submit" name="Submit" value="Insert" class="btn">
</form>

尝试发布 -

我创建了 test.js 文件并在其中编写了post方法 -

exports.list = function (req, res) {
    req.getConnection(function (err, connection) {
        console.log(con)
        app.post("/create", function (req, res) {
            var username = req.body.username,
     password = req.body.password;
            console.log(username);
            console.log(password);

            connection.query('INSERT INTO users(email,password) VALUES', (username, password), function (err, rows) {

                if (error) {

                    console.log(error.message);
                } else {
                    console.log('succes');

                }

            });

        });
    });
}

但是这没用。 我尝试在主 server.js 文件中编写post方法 -

app.post("/create", function (req, res) {
        var username = req.body.username,
 password = req.body.password;
        console.log(username);
        console.log(password);

        connection.query('INSERT INTO users(email,password) VALUES', (username, password), function (err, rows) {

            if (error) {

                console.log(error.message);
            } else {
                console.log('succes');

            }

        });

    });

但这也不起作用。

我正在关注 server.js 文件中的当前设置 -

/**
* Module dependencies.
*/

var express = require('express')
  , routes = require('./routes')
  , index_form = require('./routes/index_form')
  , user = require('./routes/user')
  , test = require('./routes/test')
  , mysql = require('mysql')
  , http = require('http')
  , path = require('path')
  , mongoose = require('mongoose');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'pass@123'
});

var app =express();

app.configure(function () {
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function () {
    app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/test', test.test);
app.get('/users', user.list);
app.get('/index_form', index_form.index_form)

http.createServer(app).listen(app.get('port'), function () {
    console.log("Express server listening on port " + app.get('port'));
});

请指导我完成这一切,如何使用node.js进行表单发布?

1 个答案:

答案 0 :(得分:1)

您是否尝试在GET路线之后将app.post("/create", test.test);放入当前的 server.js ?因为我在这里看到你当前的 server.js 没有任何POST请求。