I am using express to build a mongodb + auth0 app.
My requests maintain their body when they are placed in index.js, but as soon as I move into another js file in the project, it returns undefined, where headers and other req attributes are maintained.
const express = require('express')
const app = express();
const port = 8000;
var loginRoutes = require('./routes/login');
app.use(express.json());
const clientPromise = require('./mongodb-client');
app.use('/login', loginRoutes);
const express = require('express')
router = express.Router();
const clientPromise = require('../mongodb-client');
const auth0 = require('../auth0-client');
const app = express();
app.use(express.json());
const {ObjectId} = require("mongodb");
//LOGIN USER
router.post('/', async function(req, res) {
console.log(JSON.stringify(req.body));
var data = {
username: req.body.username,
password: req.body.password
};
auth0.passwordGrant(data, function (err, userData) {
if (err) {
// Handle error.
}
console.log(userData);
});
res.send({'login': 'yes'})
})
module.exports = router;
I'm assuming it has something to do with express.json() and maybe some OOO issue? But I'm drawing on the search engines and can't figure out what to troubleshoot next.
app.use(express.json());
must come before var loginRoutes = require('./routes/login');
for the request body to parse and be successfully passed to the child login route.