Search code examples
mysqlnode.jsexpressejsembedded-javascript

display article in database but it has html tag


I'm creating a web using nodejs, ExpressJS, EJS and MySQL. I save some data with HTML code in MySQL. When I get data from MySQL and show in web,an error occur: HTML tag in data also showing with data. How can you help me solve this problem. Thanks.


Solution

  • Try to pass your output through a function such as:

    function escapeHtml(unsafe) {
        return unsafe
             .replace(/&/g, "&")
             .replace(/</g, "&lt;")
             .replace(/>/g, "&gt;")
             .replace(/"/g, "&quot;")
             .replace(/'/g, "&#039;");
     }
    

    Shamelessly copied from here.