Skip to content

Instantly share code, notes, and snippets.

View Wanuja97's full-sized avatar
🏠
Working from home

Wanuja Ranasinghe Wanuja97

🏠
Working from home
View GitHub Profile
@Wanuja97
Wanuja97 / post.controller.ts
Created February 24, 2024 10:43
SRP-compliant Approach: PostController
import { Controller, Get, Post, Body} from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
// endpoint for create new post
@Post()
@Wanuja97
Wanuja97 / users.controller.ts
Created February 24, 2024 10:40
SRP-compliant Approach: UsersController
import { Controller, Get, Post, Body} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
// endpoint for create new user
@Post()
@Wanuja97
Wanuja97 / userposts.controller.ts
Last active February 24, 2024 10:31
Non-SRP Approach: UserPosts Controller
import { Controller, Get, Post, Body } from '@nestjs/common';
import { UserpostsService } from './userposts.service';
import { CreateUserDto } from 'src/users/dto/create-user.dto';
import { CreatePostDto } from 'src/posts/dto/create-post.dto';
/*
This approach is not recommended because this controller class is
responsible for handling users and posts endpoints.............
Violates Single Responsibility Principle.....................
*/
@Wanuja97
Wanuja97 / ISR.tsx
Created September 22, 2023 14:01
Incremental Static Regeneration in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`, { next: { revalidate: 20 } })
return res.json()
}
export default async function ISR() {
const result = await getDateTime()
@Wanuja97
Wanuja97 / SSG.tsx
Created September 22, 2023 14:00
Static Site Generation in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`,{cache: 'force-cache'})
return res.json()
}
export default async function SSG() {
const result = await getDateTime()
const dateTime = result.datetime
@Wanuja97
Wanuja97 / SSR.tsx
Created September 22, 2023 13:58
Server Side Rendering in Next.js
import React from 'react'
import TimeCard from './TimeCard'
async function getDateTime() {
const res = await fetch(`https://worldtimeapi.org/api/ip`, { cache: 'no-store' })
return res.json()
}
export default async function SSR() {
const result = await getDateTime()
@Wanuja97
Wanuja97 / CSR.tsx
Created September 22, 2023 13:55
Client Side Rendering - Next.js
'use client'; // Disable server-side rendering with this statement in the component file
import React from 'react'
import TimeCard from './TimeCard'
import axios from 'axios';
import { useState, useEffect } from 'react'
export default function CSR() {
const [dateTime, setDateTime] = useState('');
useEffect(() => {
axios
.get('https://worldtimeapi.org/api/ip') // Make a request for take the date and time from the API
@Wanuja97
Wanuja97 / index.js
Created May 8, 2023 11:36
How to send emails in nodejs + express application - Part 03
// Creating an endpoint for sending customized emails
app.post('/customized-email',(req, res)=>{
let config = {
service: 'gmail',
auth: {
user: process.env.NODEJS_GMAIL_APP_USER,
pass: process.env.NODEJS_GMAIL_APP_PASSWORD
}
}
let transporter = nodemailer.createTransport(config);
@Wanuja97
Wanuja97 / index.js
Created May 8, 2023 10:34
How to send emails in nodejs + express application - Part 2
const express = require('express');
const nodemailer = require('nodemailer');
const Mailgen = require('mailgen');
require('dotenv').config();
const app = express();
const port = 3001;
app.use(express.json());
app.post('/email',(req, res)=>{
@Wanuja97
Wanuja97 / index.js
Last active May 8, 2023 12:21
Send emails in nodejs express application - Part 01
const express = require('express');
const app = express();
const port = 3001;
require('dotenv').config();
app.use(express.json());
app.post('/email',(req, res)=>{
})