I can hit this endpoint http://localhost:8000/api/signup
via Postman and it returns JSON
POST http://localhost:8000/api/signup
{
"name": "Username",
"email": "username@mail.co",
"password": "abcedf"
}
The response
{
"message": "Signup success! Please signin"
}
In my frontend (built with next.js) I am trying to use fetch
to do the same thing, but I get 3 errors:
GET http://localhost:8000/api/signup net::ERR_ABORTED 404 (Not Found)
SyntaxError: Unexpected end of input at eval (auth.js?8ae0:18) "error"
Uncaught (in promise) TypeError: Cannot read property 'error' of undefined
config.js
import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
export const API = publicRuntimeConfig.PROD ? 'https://production.website.com' : 'http://localhost:8000'
export const APP_NAME = publicRuntimeConfig.APP_NAME
auth.js
import fetch from 'isomorphic-fetch'
import { API } from '../config'
export const signup = user => {
// return fetch(`${API}/signup`,
return fetch(`http://localhost:8000/api/signup`,
{ mode: 'no-cors'},
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(user)
})
.then(res => {
return res.json()
})
// .then(data => console.log('success:', data))
.catch(err => console.log(err, 'error'))
}
The submit function from SignupComponent.js
const handleSumbit = e => {
e.preventDefault()
// console.table({ name, email, password, error, loading, message, showForm })
setValues({...values, loading: true, error: false})
const user = { name, email, password }
signup(user)
.then(data => {
if(data.error) {
setValues({ ...values, error: data.error, loading: false })
} else {
setValues({...values, name: '', email: '', password: '', error: '', loading: false, message: data.message, showForm: false})
}
})
}
Backend Server Console each time I trigger handleSubmit
GET /api/signup 404 0.417 ms - 149
GET /api/signup 404 0.516 ms - 149
GET /api/signup 404 0.499 ms - 149
GET /api/signup 404 0.313 ms - 149
What am I missing?