Categories
Angular Answers

How to bind HTML with Angular?

Sometimes, we want to bind HTML with Angular.

In this article, we’ll look at how to bind HTML with Angular.

How to bind HTML with Angular?

To bind HTML with Angular, we use [innerHTML].

For instance, we write

<div [innerHTML]="htmlString"></div>

to render the contents of htmlString as raw HTML.

We use [innerHTML] to make render htmlString as is instead of escaping the string before rendering it.

Conclusion

To bind HTML with Angular, we use [innerHTML].

Categories
React Answers

How to intercept and handle browser’s back button in React Router v6?

Sometimes, we want to intercept and handle browser’s back button in React Router v6.

In this article, we’ll look at how to intercept and handle browser’s back button in React Router v6.

How to intercept and handle browser’s back button in React Router?

To intercept and handle browser’s back button in React Router, we can listen for changes in the history object with the navigation.listen method.

For instance, we write

import { BrowserHistory } from "history";
import React, { useContext } from "react";
import { UNSAFE_NavigationContext } from "react-router-dom";

export default function usePathname() {
  const [state, setState] = React.useState(window.location.pathname);

  const navigation = useContext(UNSAFE_NavigationContext)
    .navigator;
  React.useLayoutEffect(() => {
    if (navigation) {
      navigation.listen((locationListener) =>
        setState(locationListener.location.pathname)
      );
    }
  }, [navigation]);

  return state;
}

to create the usePathname hook to listen for for navigation with navigation.listen.

We call it with a callback to listen for navigation.

In it, we call setState with locationListener.location.pathname to save the latest URL we navigated to as the value of state.

Conclusion

To intercept and handle browser’s back button in React Router, we can listen for changes in the history object with the navigation.listen method.

Categories
JavaScript Answers

How to fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript?

To fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript, we can change the config of the cors middleware.

For instance, we write

const app = express();
const corsConfig = {
  credentials: true,
  origin: true,
};
app.use(cors(corsConfig));

to set the credentails and origin options to true when we add the cors middleware in our Express app to avoid this error.

Categories
JavaScript Answers

How to get HTTP method in controller with Express.js?

Sometimes, we want to get HTTP method in controller with Express.js and JavaScript.

In this article, we’ll look at how to get HTTP method in controller with Express.js and JavaScript.

How to get HTTP method in controller with Express.js and JavaScript?

To get HTTP method in controller with Express.js and JavaScript, we can use the req.method property.

For instance, we write

const register = (req, res) => {
  if (req.method == "POST") {
    //...
  }
  res.render('user/registration.html', {
    form: form.toHTML()
  });
}

app.get('/register', register)
app.post('/register', register)

to define the register route handler function.

In it, we check the HTTP method used to make the request with req.method.

Then we can use the same route handler function for routes with different methods.

Conclusion

To get HTTP method in controller with Express.js, we can use the req.method property.

Categories
JavaScript Answers

How to get rid of X-Powered-By response header in Node.js Express and JavaScript?

Sometimes, we want to get rid of X-Powered-By response header in Node.js Express and JavaScript.

In this article, we’ll look at how to get rid of X-Powered-By response header in Node.js Express and JavaScript.

How to get rid of X-Powered-By response header in Node.js Express?

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.

For instance, we write

app.disable('x-powered-by');

to disable the 'x-powered-by' option which removes the X-Powered-By response header in our Express app.

Conclusion

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.