Answer the question
In order to leave comments, you need to log in
Why body-parser only works in app.js?
I figured it was my mistake.
The connection to the database must be moved to a separate file and connected in the file with the request.
The bodyparser has nothing to do with it, the request to the database did not pass.
----------------------------
Hello.
The situation is this, I include a bodyparser in app.js
const express = require('express')
const mongoose = require('mongoose')
const pg = require('pg')
const bodyParser = require('body-parser')
const authRoutes = require('./routes/auth')
const analyticsRoutes = require('./routes/analytics')
const categoryRoutes = require('./routes/category')
const orderRoutes = require('./routes/order')
const positionRoutes = require('./routes/position')
const keys = require('./lib/config/keys')
const app = express()
const pgdb = new pg.Pool(keys.pgPool)
pgdb.connect()
.then(() => console.log('PG Connected'))
.catch(error => console.log(error))
app.use(require('morgan')('dev'))
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.use(require('cors')())
app.use('/api/auth', authRoutes)
module.exports = app
app.post('/register', async (req, res) => {
const obj = req.body
const queryReg = 'INSERT INTO public.users(email, password) VALUES ($1, $2)'
await pgdb.query(queryReg, [obj.email, obj.password])
res.send('ok')
}
const authRoutes = require('./routes/auth')
const express = require('express')
const controller = require('../controllers/auth')
const router = express.Router()
router.post('/login', controller.login)
router.post('/register', controller.register)
module.exports = router
module.exports.register = async (req, res) => {
const obj = req.body
const queryReg = 'INSERT INTO public.users(email, password) VALUES ($1, $2)'
await pgdb.query(queryReg, [obj.email, obj.password])
res.send('ok')
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question