how to use static file node js


app.use(express.static(__dirname + '/public'));
<link rel="stylesheet" type="text/css" href="css/style.css" />

app.use('/static', express.static(path.join(__dirname, 'public')))

app.use('/static', express.static('public'))

app.use(express.static('public'))
app.use(express.static('files'))

www.codegrepper.com
www.stackoverflow.com

how to use session using javascript


BY LOVE
sessionStorage.setItem("SessionName", "SessionValue");

<script type="text/javascript">
function GetUserName()
{

var username = '<%= Session["UserName"] %>';
alert(username );
}
</script>

<script type="text/javascript">
function SetUserName()
{
var userName = "Shahid Bhat";
'<%Session["UserName"] = "' + userName + '"; %>';
alert('<%=Session["UserName"] %>');
}
</script>

www.codegrepper.com
www.stackoverflow.com

how to use request module in node js


const request = require('request');
request('http://www.google.com', function (error, response, body) {
console.error('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});

www.codegrepper.com
www.stackoverflow.com

how to use reduce javascript


function flattenArray(data) {
// our initial value this time is a blank array
const initialValue = [];

// call reduce on our data
return data.reduce((total, value) => {
// if the value is an array then recursively call reduce
// if the value is not an array then just concat our value
return total.concat(Array.isArray(value) ? flattenArray(value) : value);
}, initialValue);
}

//note idx and sourceArray are optional
const sum = array.reduce((accumulator, element[, idx[, sourceArray]]) => {
//arbitrary example of why idx might be needed
return accumulator + idx * 2 + element
}, 0);

const sum = array.reduce((accumulator, element) => {
return accumulator + element;
}, 0);
// An example that will loop through an array adding
// each element to an accumulator and returning it
// The 0 at the end initializes accumulator to start at 0
// If array is [2, 4, 6], the returned value in sum
// will be 12 (0 + 2 + 4 + 6)

const product = array.reduce((accumulator, element) => {
return accumulator * element;
}, 1);
// Multiply all elements in array and return the total
// Initialize accumulator to start at 1
// If array is [2, 4, 6], the returned value in product
// will be 48 (1 * 2 * 4 * 6)

www.codegrepper.com
www.stackoverflow.com

how to use reduce in javascript


const sum = array.reduce((accumulator, element) => {
return accumulator + element;
}, 0);
// An example that will loop through an array adding
// each element to an accumulator and returning it
// The 0 at the end initializes accumulator to start at 0
// If array is [2, 4, 6], the returned value in sum
// will be 12 (0 + 2 + 4 + 6)

const product = array.reduce((accumulator, element) => {
return accumulator * element;
}, 1);
// Multiply all elements in array and return the total
// Initialize accumulator to start at 1
// If array is [2, 4, 6], the returned value in product
// will be 48 (1 * 2 * 4 * 6)

var myNewArray = myArray.reduce(function(prev, curr) {
return prev.concat(curr);
});
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

//note idx and sourceArray are optional
const sum = array.reduce((accumulator, element[, idx[, sourceArray]]) => {
//arbitrary example of why idx might be needed
return accumulator + idx * 2 + element
}, 0);

www.codegrepper.com
www.stackoverflow.com

how to use redirect in react


state = { redirect: null };

render() {
if (this.state.redirect) {
return <Redirect to={this.state.redirect} />
}
return(
// Your Code goes here
)
}

// update the redirect
this.setState({ redirect: "/someRoute" });

import { Route, Redirect } from 'react-router'

<Route exact path="/" render={() => (
loggedIn ? (
<Redirect to="/dashboard"/>
) : (
<PublicHomePage/>
)
)}/>

state = { redirect: null };
render() {
if (this.state.redirect) {
return <Redirect to={this.state.redirect} />
}
return(
// Your Code goes here
)
}

from django.shortcuts import redirect

import { useHistory } from "react-router-dom";

function App() {
let history = useHistory();
}

www.codegrepper.com
www.stackoverflow.com

how to use react router


npm install --save react-router-dom

import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";

export default function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/users">Users</Link>
</li>
</ul>
</nav>

$ npm install --save react-router

import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";

export default function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/users">Users</Link>
</li>
</ul>
</nav>

{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/users">
<Users />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}

function Home() {
return <h2>Home</h2>;
}

function About() {
return <h2>About</h2>;
}

function Users() {
return <h2>Users</h2>;
}

npm install react-router-dom

www.codegrepper.com
www.stackoverflow.com

how to use react memo hooks


function moviePropsAreEqual(prevMovie, nextMovie) {
return prevMovie.title === nextMovie.title
&& prevMovie.releaseDate === nextMovie.releaseDate;
}

const MemoizedMovie2 = React.memo(Movie, moviePropsAreEqual);

const computeLetterCount = word => {
let i = 0;
while (i < 1000000000) i++;
return word.length;
};

// Memoize computeLetterCount so it uses cached return value if input array ...
// ... values are the same as last time the function was run.
const letterCount = useMemo(() => computeLetterCount(word), [word]);

www.codegrepper.com
www.stackoverflow.com