I have cors issue in my code even tho there were no problems when everything was running on local host
Code on the server
import bodyParser from 'body-parser';
import bcrypt from 'bcrypt-nodejs';
import cors from 'cors';
import knex from 'knex';
import handleRegister from './controllers/register.js';
import handleSignin from './controllers/signin.js';
import handleProfileGet from './controllers/profile.js';
import {handleImage, handleApiCall} from './controllers/image.js';
const db = knex({
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
const app = express();
app.use(bodyParser.json());
app.use(cors(
origin: '*',
credentials:true
));
app.get('/', (req,res) => {res.send('it is working')})
app.post('/signin', (req, res) => { handleSignin(req, res, db, bcrypt) })
app.post('/register', (req, res) => { handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { handleApiCall(req, res)})
app.listen(process.env.PORT || 3000, () => {
console.log(`app is running on port ${process.env.PORT}`);
})
my post request
onSubmitSignIn = () => {
fetch('https://intense-lowlands-80632.herokuapp.com/signin', {
method:'post',
headers:{'Content-Type': 'application/json'},
body:JSON.stringify({
email: this.state.signInEmail,
password: this.state.signInPassword
})
})
.then(response => response.json())
.then(user => {
if (user.id) {
this.props.loadUser(user)
this.props.onRouteChange('home')
}
})
}
error
2021-09-21T10:03:17.603744+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=OPTIONS path="/signin" host=intense-lowlands-80632.herokuapp.com request_id=b146be27-0bd2-43ac-bcb9-b0661e6a3085 fwd="188.25.95.60" dyno= connect= service= status=503 bytes= protocol=https