app.post不工作​​node.js

时间:2014-01-14 17:07:04

标签: node.js express pug

我是节点新手,我想尝试一个简单的app.post,但我无法让它工作。我的app.js和index.jade代码如下所示。当我在表单中输入数据并按下提交时,我试图让我的应用程序将“hi”打印到控制台,但这不会发生。

**app.js**

/**enter code here
 * Module dependencies.
 */

var express = require('express');

var routes = require('./routes');

var user = require('./routes/user');

var http = require('http');

var path = require('path');

var app = express.createServer();

app.use(express.bodyParser());

// all environments

app.set('port', process.env.PORT || 3000);

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'jade');

app.use(express.favicon());

app.use(express.logger('dev'));

app.use(express.json());

app.use(express.urlencoded());

app.use(express.methodOverride());

app.use(app.router);

app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);

app.get('/users', user.list);

app.get('/george', function(req,res){
    res.send('This is the random george page');
    console.log("george");
});

app.get('/second', function(req,res){
    res.render('secondpage');
});

app.get('/act', function(request, response){
    console.log("hello");
});

app.post('/', function(request, response){
    console.log("hi");
});

app.listen(3000);
console.log("Express server listening on port 3000");





**index.jade**

extends layout

block content

  h1: a(href = 'second') George

  p Welcome to your demosite George


  form(method="post", action="/", name="act")

  p

  |Username

  input(type="text", name="user")

  p

  |Password

  input(type="text", name="pass")

  p

  input(type="submit", value="Submit")

1 个答案:

答案 0 :(得分:3)

form标记需要另外两个前导空格缩进后,首先猜测是jade文件中的所有内容,以确保input标记最终嵌套在HTML中的form标记内。您的快速JS代码看起来应该可以正常工作。