我不知道如何解决这个问题,我做了一个表格并尝试做了#34;发布"使用参数" name",但我得到" undefined",尝试了互联网上的一切,但无法解决
档案:app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var fs = require('fs');
var routes = require('./routes/index');
var almuerzo = require('./routes/almuerzo');
var app = express();
//DB Connect
mongoose.connect("mongodb://localhost/comedorDB");
//Load all files in models dir
fs.readdirSync(__dirname + '/models').forEach(function (filename) {
if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename);
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/comida', almuerzo);
file:almuerzo.js
var express = require('express')
var router = express.Router();
/* GET users listing. */
router.get('/', function (request, response) {
response.render('comida');
});
router.post("/", function (req,resp) {
resp.send('nombre: '+req.body.nombre);
// resp.render("comida");
});
module.exports = router;
当我尝试发送"发布"形式postman chrome扩展它的工作原理(内容类型:application / x-www-form-urlencoded)但是当我尝试从我的表单:
extends layout
block takami
.container-fluid
.row
.col-md-12
h3 Guardar almuerzo
form(method="post", action="/comida")
fieldset.form-group
label(for='nombre') Nombre de Comida
input#nombre.form-control(type='text', placeholder='Nombre')
fieldset.form-group
label(for='tipo') Tipo de comida
input#tipo.form-control(type='text', placeholder='tipo de comida')
button.btn.btn-primary(type='submit') Submit
这是我的Chrome浏览器标题信息:
POST /comida HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 0
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://localhost:3000
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:3000/comida
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,es;q=0.8