2017-04-03 2 views
1

저는 백엔드 자바 스크립트에 익숙하며 제출시 제출 된 개체를 표시하도록되어 있지만 대신 빈 개체를 표시합니다. 나는 body-parser를 포함 시켰기 때문에 문제가 어디에 있는지 잘 모르겠습니다. 여기 내 app.js 파일입니다빈 개체를 반환하는 req.body

const express = require("express"); 
const app = express(); 
const mustacheExpress = require("mustache-express"); 
const bodyParser = require("body-parser"); 
const pgp = require("pg-promise")(); 

app.engine("html", mustacheExpress()); 
app.set("view engine", "html"); 
app.set("views", __dirname + "/views"); 
app.use("/", express.static(__dirname + "/public")); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); 

app.get('/new_creature',function(req,res){ 
    res.render('new_creature/index'); 
}); 
app.post('/new_creature', function(req,res){ 
    res.end(JSON.stringify(req.body)); 
    console.log(req.body); 
    // res.render('new_creature/index'); 
}); 

여기 내보기/생물/new_creature/index.html을 파일 : 당신이 bodyparser은 당신이해야 할 일을하려면

<form method="POST" action="/new_creature"> 
    Species:<input type="text"> 
    Family:<input type="text"> 
    Habitat:<input type="text"> 
    Diet:<input type="text"> 
    Planet:<input type="text"> 
    <input type="submit" placeholder="submit"> 
</form> 

답변

4

이 이름에 대한 속성 게시 할 양식 요소 예를 들면 다음과 같습니다.

<form method="POST" action="/new_creature"> 
    Species:<input name='species' type="text"> 
    Family:<input name='family' type="text"> 
    Habitat:<input name='habitat' type="text"> 
    Diet:<input name='diet' type="text"> 
    Planet:<input name='planet' type="text"> 
    <input type="submit" value="submit"> 
</form> 

또한 제출 단추에는 자리 표시자가 적합하지 않습니다. 자리 표시자는 입력되지 않은 입력에 표시해야하는 유령 텍스트입니다.

+0

감사합니다. –