无法 POST 表单 node.js - express

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14902923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:10:54  来源:igfitidea点击:

Cannot POST form node.js - express

node.jsexpress

提问by baba-dev

Using express 3.1.0 I have a super simple form:

使用 express 3.1.0 我有一个超级简单的形式:

<form action="/signup" method="post">
    <div>
        <label>Username:</label>
        <input type="text" name="username"/><br/>
    </div>
    <div>
        <label>Password:</label>
        <input type="password" name="password"/>
    </div>
    <div><input type="submit" value="Sign Up"/></div>
</form>

and in the app.js:

在 app.js 中:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , fs = require('fs')
  , User = require('./models/User.js')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

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(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('/form', function(req, res) {
    fs.readFile('./form.html', function(error, content) {
        if (error) {
            res.writeHead(500);
            res.end();
        }
        else {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.end(content, 'utf-8');
        }
    });
});

app.post('/signup', function(req, res) {

    var username = req.body.username;
    var password = req.body.password;
    User.addUser(username, password, function(err, user) {
        if (err) throw err;
        res.redirect('/form');
    });
});

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


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

when trying to post this form i'm getting:

尝试发布此表单时,我得到:

Cannot POST /signup

and in the console:

并在控制台中:

"NetworkError: 404 Not Found - http://localhost:3000/signup"

what am i missing here?

我在这里错过了什么?

回答by Hector Correa

Your example works for me. I removed the references to User, user, and routes so that I can run it and the HTTP POST is received and displayed correctly in the console.

你的例子对我有用。我删除了对用户、用户和路由的引用,以便我可以运行它并在控制台中正确接收和显示 HTTP POST。

app.post('/signup', function(req, res) {
    var username = req.body.username;
    var password = req.body.password;
    console.log("post received: %s %s", username, password);
});

I suspect the error is in your User.addUser() code.

我怀疑错误出在您的 User.addUser() 代码中。

回答by salangkar acharya

router.route('/signup')

    // (accessed at POST http://localhost:3000/api/signup)
    .post(function(req, res) {
        var username = req.body.username;
        var password = req.body.password;
        res.json(
            { 
               message: 'signup success',
                username : username,
                 password : password,
            }
        );
    })
    .get(function(req,res){
        res.json({message: 'get request from signup'});
});

// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);

回答by sansan

You can write something like this:

你可以这样写:

action="http://localhost:3000/sin"