2013-02-15 62 views
21

使用快速3.1.0。我有一個超級簡單的形式:無法發佈形式的node.js - 表達

<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> 

,並在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')); 
}); 

試圖發佈此表單時我越來越:

Cannot POST /signup 

,並在控制檯:

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

我在這裏錯過了什麼?

回答

21

你的例子適合我。我刪除了對用戶,用戶和路由的引用,以便我可以運行它並在控制檯中正確接收並顯示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); 
}); 

我懷疑錯誤發生在您的User.addUser()代碼中。

+0

正確。該問題的確在User.addUser()中。謝謝 – 2013-02-15 21:10:47

+0

浪費了6個多小時後,終於找到了我要找的解決方案。謝謝 – Toretto 2015-01-16 09:19:53

0

你可以寫這樣的事情:

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

只要重新啓動您的應用程序,每一件事情都會好起來去;)

都發生了什麼事是你的應用程序運行時,您已經添加了一些代碼。爲了使新代碼的效果只是重新運行您的節點應用程序

+5

哦!這個答案 - 呃有膽量 – rneha725 2016-03-22 16:54:40

6
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); 
+6

嘗試解釋代碼,而不是隻發佈它。它有什麼作用?你如何或在哪裏解決TC的問題? – 2014-11-05 11:25:33

+1

@NahuelIanni謝謝。 :)其實我只是寫了他得到錯誤的部分。 – 2014-11-16 06:23:00