Working with headers in Express
HTTP headers are a common way for web servers to share information about their content with other servers and web applications. Reading and writing headers can be necessary for a variety of web server needs, including cross-origin resource sharing (CORS).
Getting ready
Let's use HTTP headers to enhance our Express route configurations. For our API routes, we will add headers to enable CORS via pre-flight OPTIONS
requests. This will come in handy if we ever need to run our API on a different domain than our Angular web application.
For our Angular routes, we will read the incoming Accept-Language
header and parse it into a locale property that we can inject into our index.html
file's locale definition. This will leverage the existing internationalization work we have for our Angular app and allow us to set the displayed language in our frontend from our backend web server using headers.
To do the parsing and injection of the script tag for our index.html
file,...