This Express app listens for POST requests to /register. It checks if the username and password are sent and if the password is long enough. If any check fails, it sends an error message. Otherwise, it confirms registration.
import express from 'express';
const app = express();
app.use(express.json());
app.post('/register', (req, res) => {
const { username, password } = req.body;
if (!username) {
return res.status(400).send('Username is required');
}
if (!password) {
return res.status(400).send('Password is required');
}
if (password.length < 6) {
return res.status(400).send('Password must be at least 6 characters');
}
res.send(`User ${username} registered successfully`);
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});