Next JS Cant find my Express Server pages
Unanswered
Asian black bear posted this in #help-forum
Asian black bearOP
I am implementing a sign up page with an express server and when the form is submitted with:
await axios.post('/api/signup', formData);
The express endpoint returns a 404 not found. How can I resolve this issue. Here is my endpoint code in server.js:
server.post('/api/signup', async (req, res) => {
const { username, email, password } = req.body;
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the MySQL database
db.query(
'INSERT INTO users (username, email, password) VALUES (?, ?, ?)',
[username, email, hashedPassword],
(err, results) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error creating user' });
return;
}
res.status(200).json({ success: true });
}
);
});
await axios.post('/api/signup', formData);
The express endpoint returns a 404 not found. How can I resolve this issue. Here is my endpoint code in server.js:
server.post('/api/signup', async (req, res) => {
const { username, email, password } = req.body;
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the MySQL database
db.query(
'INSERT INTO users (username, email, password) VALUES (?, ?, ?)',
[username, email, hashedPassword],
(err, results) => {
if (err) {
console.error(err);
res.status(500).json({ error: 'Error creating user' });
return;
}
res.status(200).json({ success: true });
}
);
});
4 Replies
Shy Albatross
you're inserting values into DB without any validation or sanitization, you should be glad you get a 404 on this TBH
your POSTing to the same host and the Express server is presumably running on a different port and the 404 is probably coming from Next.js not Express
@Shy Albatross your POSTing to the same host and the Express server is presumably running on a different port and the 404 is probably coming from Next.js not Express
Asian black bearOP
Its not running on another port and this just for testing not production
@Shy Albatross your POSTing to the same host and the Express server is presumably running on a different port and the 404 is probably coming from Next.js not Express
Asian black bearOP
How can I resolve this error?