2013-10-23 104 views
0

如果我在首頁實現表單處理,系統顯示錯誤。app.post快速入門 - Node.js

守則快遞

var http = require('http'), 
express = require('express'), 
app = express(); 

app.use(express.bodyParser()); 


// A route for the home page - and the page with a form 

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

// Code for Handling the form 

}); 

表:

<form action="/" method="post"> . . . </form> 

現在我得到這個錯誤:

不能得到/

我該如何解決這個問題?一些想法?

爲完整的路由更新 - 與翡翠

// Set the view engine 
app.set('view engine', 'jade'); 
// Where to find the view files 
app.set('views', './views'); 
// A route for the home page - will render a view 
    app.post('/', function(req, res) { 
    res.render('index');  
}); 

這裏的表單代碼:

<form action="/" method="post"> 
<label>Name</label> 
<input type="text" name="name"> 
<label>Text</label> 
<textarea name="text" rows="10"></textarea> 
<input type="submit"> 
</form> 
+0

向我們顯示主頁和'帶表單的頁面'的路線。 – robertklep

回答

1

您需要手柄GET請求,以及POST請求。您需要如下所示的內容:

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

app.post('/', function (req, res, next) { 
    // Code for handle the form data 
}); 
+0

謝謝 - 我會試試看。 – user1743269