This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import { config } from 'dotenv'; | |
import { authRouter } from './src/routes/authRouter.js'; | |
config(); | |
const app = express(); | |
app.use(express.json()); | |
const port = process.env.PORT ?? 3000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nodemailer from 'nodemailer'; | |
import { config } from 'dotenv'; | |
config() | |
export const emailSender = async (to, firstName, token) => { | |
const trasporter = await nodemailer.createTransport({ | |
host: process.env.MAILGUN_SMTP_HOSTNAME, | |
port: process.env.SMTP_PORT, | |
secure: false, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { generateToken } from '../utils/jwtHelper.js'; | |
import { userRepository } from '../repository/user.js'; | |
import { emailSender } from '../utils/emailTransporter.js'; | |
const createAccount = async (req, res) => { | |
try { | |
const { firstName, lastName, email, userName } = req.body; | |
// Fetch user information from Redis | |
const existingUser = await userRepository |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Router } from 'express'; | |
import { | |
createAccount, | |
login, | |
verifyUser, | |
} from '../controllers/authController.js'; | |
import { authGuard } from '../middleware/index.js'; | |
const authRouter = Router(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Entity, Schema } from 'redis-om'; | |
import { redisClient } from '../db/index.js'; | |
class UserRepository extends Entity {} | |
const userSchema = new Schema(UserRepository, { | |
firstName: { type: 'string' }, | |
lastName: { type: 'string' }, | |
email: { type: 'string' }, | |
userName: { type: 'string' }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ulid } from 'ulid'; | |
import { hash, compare } from 'bcrypt'; | |
import { redisClient } from '../db/index.js'; | |
import { generateToken } from '../utils/jwtHelper.js'; | |
const createAccount = async (req, res) => { | |
try { | |
const { firstName, lastName, email, password, displayName } = req.body; | |
const userId = ulid(); // Generate id for the user which will be used as the key in Redis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { config } from 'dotenv'; | |
import { Client } from 'redis-om'; | |
config(); | |
const redisCloudConnectionString = `redis://${process.env.REDIS_DB_USER}:${process.env.REDIS_DB_PASS}@${process.env.REDIS_DB_URL}`; | |
const redisClient = new Client(); | |
await redisClient.open(redisCloudConnectionString); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express'; | |
import { config } from 'dotenv'; | |
config(); | |
const app = express(); | |
app.use(express.json()); | |
const port = process.env.PORT ?? 3000; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A better way of reading file that doesn't block the event loop | |
const fs = require('fs'); | |
fs.readFile('countries.csv', (error, data) => { | |
if (error) { | |
throw new Error(error); | |
} | |
console.log(data.toString()); | |
}); | |
// Other JavaScript code below this line will not be blocked which is good. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Event loop blocking code sample | |
*/ | |
const fs = require('fs'); | |
const countries = fs.readFileSync('countries.csv'); | |
console.log(countries.toString()); | |
// Other JavaScript code below this line will be blocked until the file reading completes. | |
console.log('Other JAVASCRIPT THAT IS BLOCKED'); |
NewerOlder