This Express app listens for POST requests to '/register'. It checks if username is a string and password is at least 6 characters. If validation fails, it sends an error. Otherwise, it confirms the data is valid.
import express from 'express';
const app = express();
app.use(express.json());
app.post('/register', (req, res) => {
const { username, password } = req.body;
if (!username || typeof username !== 'string') {
return res.status(400).send('Username is required and must be a string');
}
if (!password || password.length < 6) {
return res.status(400).send('Password is required and must be at least 6 characters');
}
res.send('Registration data is valid');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});