<% if locals.msg { %> <h2><%=msg %></h2> <% } else {%> <h2>There is no messages</h2> <% } %>
You need use locals when the message is empty because ejs treat it as a variable. So that is why you get server 500 error.