Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You may need to require route after calling app.use(bodyParser) #22

Closed
rs459 opened this issue May 4, 2014 · 3 comments
Closed

You may need to require route after calling app.use(bodyParser) #22

rs459 opened this issue May 4, 2014 · 3 comments

Comments

@rs459
Copy link

rs459 commented May 4, 2014

if you do this :

[...]
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '{views}');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());{css}
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
[...]

req.body is always empty if you submit form (POST) via the html way , because in your files settings each route, you call express.Router() before settings bodyparser.

@defunctzombie
Copy link
Contributor

I don't understand what you are trying to say. Please clarify what doesn't work or what you are trying to do. Valid app code we can run and see fail would be helpful.

@rs459
Copy link
Author

rs459 commented May 6, 2014

Sorry,

change route/users.js to this :

/* GET users listing. */
router.get('/', function(req, res) {
  res.render('users');
});

router.post('/', function(req, res) {
    console.log(req.body.userName);
    res.end();
});

create users.jade

extend layout

block content
    form(id="createUser", action='/users', method="POST")
        label(for="userName") Username : 
        input(id="userName", type="text")
        input(type="submit")

console.log should return a value, you instantiate Router when you require your routes files, you do this before 'bodyparser'. That's why console.log(req.body.name) return 'undefined'.

@defunctzombie
Copy link
Contributor

This issue still makes no sense to me and I don't see what changes need to happen. If you make a PR I can review that, but based on the description I don't see a problem (looking at the current generator code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants