fullstackreact / food-lookup-demo

A demonstration of using `create-react-app` with a server
MIT License
1.19k stars 373 forks source link

Cannot GET /register on heroku server error #60

Open purveshshende2 opened 4 years ago

purveshshende2 commented 4 years ago

I Cannot GET /register on heroku server error , it send 404 bad request

server.js

`const express = require('express'); const bodyParser = require('body-parser'); const bcrypt = require('bcrypt'); const cors = require('cors'); const knex = require('knex'); const register = require ('./controllers/register'); const signin = require('./controllers/signin'); const profile = require('./controllers/profile'); const image = require('./controllers/image');

const db = knex({ client: 'pg', connection: { connectionString: process.env.DATABASE_URL, ssl: true, } });

const app = express(); app.use(cors()) app.use(bodyParser.json());

app.get('/',(req,res) => {res.send('it is Working!')})
app.post ('/signin', signin.handleSignin(db,bcrypt)) app.post('/register',(req,res) => { register.handleRegister(req,res,db,bcrypt)}) app.get('/profile/:id',(req,res) => {profile.handleProfileGet(req,res,db)}) app.put('/image',(req,res) => {image.handleImage(req,res,db)}) app.post('/imageurl',(req,res) => {image.handleApiCall(req,res)})

app.listen(process.env.PORT || 3000, () => { console.log(app is running on port ${process.env.PORT}); })`

Register.js

`

const handleRegister = (req,res,db,bcrypt) => { const {email,name,password} = req.body; if (!email || !name || !password){ return res.status(400).json('incorrect form submission'); } const saltRounds = 10; const hash = bcrypt.hashSync(password,saltRounds); const salt = bcrypt.genSaltSync(saltRounds); db.transaction(trx => { trx.insert({ hash : hash, email : email }) .into ('login') .returning('email') .then(loginEmail => { return trx('users1') .returning('*') .insert ({ email : loginEmail[0], name : name, joined : new Date() }) .then (user => { res.json(user[0]); }) }) .then(trx.commit) .catch(trx.rollback) })

.catch(err => res.status(400).json('unable to register'))

}

module.exports = { handleRegister : handleRegister }; ` server_file.txt registerfile.txt