Useful code snippets for projects.
Hashing and verifying a password using bcrypt.
const bcrypt = require('bcrypt');
async function hashPassword(password) {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
console.log(hashedPassword);
const isMatch = await bcrypt.compare(password, hashedPassword);
console.log('Password match:', isMatch);
}
hashPassword('my_secure_password');
A basic Express server that serves a home route.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
An input field that validates the email format in real-time.
import { useState } from 'react';
import { TextInput, View, Text } from 'react-native';
export default function EmailInput() {
const [email, setEmail] = useState('');
const isValid = /\S+@\S+\.\S+/.test(email);
return (
<View>
<TextInput
placeholder="Enter your email"
value={email}
onChangeText={setEmail}
style={{
borderWidth: 1,
borderColor: isValid ? 'green' : 'red',
padding: 10,
borderRadius: 5,
}}
/>
{!isValid && email.length > 0 && (
<Text style={{ color: 'red', marginTop: 5 }}>Invalid email</Text>
)}
</View>
);
}
A button that triggers different actions for short and long presses.
import { TouchableOpacity, Text, Alert } from 'react-native';
export default function LongPressButton() {
return (
<TouchableOpacity
onPress={() => Alert.alert('Short press!')}
onLongPress={() => Alert.alert('Long press!')}
style={{
backgroundColor: 'blue',
padding: 15,
borderRadius: 8,
alignItems: 'center',
}}
>
<Text style={{ color: 'white', fontWeight: 'bold' }}>Press Me</Text>
</TouchableOpacity>
);
}