Chapter 5. Data Sources and Flow Control

Up until now, the client has been able to get whatever it needs more or less immediately. We wait for our end user to initiate a request, but that user never waits for the application. Of course, that isn’t how real web applications work. Dynamic data requires some sort of data storage, and working with a data store takes time. When our user initiates a request to the server, the application on the server may, in turn, need to initiate its own request to a database, and will not be able to respond to the user until a response comes from the data source. While connecting to a database is a necessary function in itself, it’s also a good place to discuss server-side flow control.

Connecting to a Database

If you have some experience dealing with databases in other server frameworks, thinking about the way it’s done in Node can be a bit tricky. Firstly, Node has no default way of connecting to a database. Depending on your experience, you may have worked with ODBC or JDBC (Open Database Connectivity and Java Database Connectivity, respectively). No analogue to those things exists in Node. A database will provide its own API and you will connect directly to that. The only thing shared between Node itself and the database will be the EventEmitter vocabulary, if that.

The second trick is that there are a lot of different ways people choose to store data in Node. If you’re used to .NET on the back-end, you probably expect to use SQL Server. ...

Get Node for Front-End Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.