app.post()不与快递工作(app.post() not working with Expre

2019-08-16 20:41发布

我得到快速的问题,我尝试使用app.post()函数,但它不工作,我不知道为什么...

虽然我包括bodyParser()...

问题:页负荷而不响应,没有错误消息。 我没有看到的console.log()...

app.js:

var express = require('express')
    , routes = require('./routes')
    , user = require('./routes/user')
    , postProvider = require('./postProvider.js')
    , http = require('http')
    , path = require('path')
    , compass = require('node-compass')
    , hash = require('./auth').hash;

var app = express();

app.configure(function () {
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(compass());
    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.use(express.cookieParser('dsqdq edsds'));
    app.use(express.session());
});

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

app.get('/admin', function(req, res){
    res.redirect('login');
});

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

app.post('/login', function(req, res){
    console.log('test');
});

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

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

login.jade:

extends layout

block content
  h1 Login
  h3 "tj" and "foobar"
  form(method="post", action="/login")
    input(type="text", placeholder="Username", autofocus="true", name="username")
    br
    input(type="password", placeholder="Password", name="password")
    br
    input(type="submit", value="login")

Answer 1:

我不觉得有app.post代码的任何问题!

app.post('/login', function(req, res){
    console.log('test');
    res.end(); // end the response
});

一个建议是,你应该结束每个响应发送到客户端,否则您的服务器不响应。



Answer 2:

您的POST请求发送到/test (因为你已经用action窗体的属性),而您明确监听器监听到任何post请求/login

你的代码应该是这样的:

block content
  h1 Login
  h3 "tj" and "foobar"
  form(method="post", action="/login")

如果你想要这个工作!



Answer 3:

我复制“router.post()......”另一个js文件,并更改app.js的路线,它的工作!不过“router.post()”方法仍然是一个warning.sometimes好像在webstorm警告,但它实际上可以工作!



文章来源: app.post() not working with Express