Skip to content

Instantly share code, notes, and snippets.

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

Sahan Amarsha SahanAmarsha

🏠
Working from home
View GitHub Profile
@SahanAmarsha
SahanAmarsha / me.gif
Last active May 28, 2020 20:25
A little description about me
me.gif
@SahanAmarsha
SahanAmarsha / projects.gif
Last active July 19, 2020 14:41
My Projects
projects.gif
@SahanAmarsha
SahanAmarsha / DenoWelcome.md
Last active May 18, 2020 08:36
Simple Deno Welcome Command
@SahanAmarsha
SahanAmarsha / Deno.ts
Created May 18, 2020 10:09
A simple server built in with Deno
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
const server = serve({ port: 5000 });
console.log("http://localhost:5000/");
for await (const req of server) {
req.respond({ body: "Hello World\n" });
}
@SahanAmarsha
SahanAmarsha / Server.js
Last active May 18, 2020 14:04
Simple Server built using NodeJs
const express = require('express');
const app = express();
app.get('/', res) => {
res.send('Hello World');
}
app.listen(3000);
@SahanAmarsha
SahanAmarsha / nodemon.json
Created May 25, 2020 05:34
How to define environmental variables in your Node.js backend
{
"env": {
"DB_USER": "Sahan",
"DB_PASSWORD": "MONGO123",
"DB_NAME": "MERN"
}
}
@SahanAmarsha
SahanAmarsha / app.js
Last active May 26, 2020 04:55
How to establish connection between MongoDB and your Node.js backend using environmental variables
const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0-usg5d.mongodb.net`+
`/${process.env.DB_NAME}?retryWrites=true&w=majority`;
mongoose
.connect( url, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
.then(() =>{
app.listen(5000);
})
.catch(err => console.log( err ));
@SahanAmarsha
SahanAmarsha / .env
Created May 25, 2020 06:48
Using environment variables in React frontend
REACT_APP_BACKEND_URL=http://localhost:5000/mern
@SahanAmarsha
SahanAmarsha / app.js
Created May 25, 2020 06:58
Fetching data using environment variables in React front-end
const response = await fetch(
`${process.env.REACT_APP_BACKEND_URL}/mern/`,
{
method: 'GET',
headers:{
'Content-Type': 'application/json'
},
}
);
@SahanAmarsha
SahanAmarsha / App.js
Last active May 26, 2020 14:27
How to lazy load components in your React App
//importing lazy and suspense from react library
import React,{lazy, Suspense} from 'react';
//lazy loading the requied components
const Home = React.lazy(() => import('./modules/pages/Home'));
const Profile = React.lazy(() => import('./modules/pages/Profile'));
const About = React.lazy(() => import('./modules/pages/About'));
//wrapping your switch compoent (inside react-router) with <Suspense>
<Router>