I have a problem with my express app. What I am doing is having a form in handelbars. I just want to register a user according to this tutorial: https://www.learn2crack.com/2016/09/android-user-registration-login-node-client.html but I am sort of adjusting it to a web app.
So I created the form like this:
<div style="text-align:center">
<form method="POST" action="/registerUser" enctype="application/x-www-form-urlencoded">
Username: <input type="text" name="name" value=""/><br>
Email: <input type="text" name="email" value=""/><br>
Password: <input type="text" name="password" value=""/><br>
Repeat password: <input type="text" name="repeat_password" value=""></input><br>
<input type="submit" value="Register!"></input>
</form>
</div
Like this. Then when I am calling a router to handle its result (registerUser) I get that req.body.name is undefined (btw I checked, req.query, req.params, etc it is all undefined. Just the content does not get there to the router. So when I do:
router.post('/registerUser', (req, res) => {
console.log("YO THERE WE GO");
console.log(req.body);
///..... rest
This does not work, it is undefined. However, if I do it in my app.js folder (routes is separate, but works with other call, which is GET) it clearly works with this simple code:
app.post('/registerUser',function(req,res){
console.log(req.body);
})
For app I have the following:
'use strict';
// app.js
const express = require('express');
const session = require('express-session');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'public')));
const router = require('./routes/router');
app.use(router);
const mongoose = require('mongoose');
const validate = require("validate.js");
const logger = require('morgan');
require('./db');
// const User = mongoose.model('User');
const Item = mongoose.model('Item');
const Pattern = mongoose.model('Pattern');
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
and for router:
'use strict';
require('../db');
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Pattern = mongoose.model('Pattern');
What is wrong that I get it using app but not using router?
Thank you in advance!
just put app.use(bodyParser.json()); before route calling like
'use strict';
const mongoose = require('mongoose');
const validate = require("validate.js");
const logger = require('morgan');
require('./db');
// const User = mongoose.model('User');
const Item = mongoose.model('Item');
const Pattern = mongoose.model('Pattern');
const router = require('./routes/router');
// app.js
const express = require('express');
const session = require('express-session');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(router);