So as I mentioned before, I'm not 100% happy with the code that has been added here to
app.js.
Whilst we could run this code like this, I would rather run this code after we started
the server.
So what I'll do instead, instead of exporting the app object here, is I'll call app.listen,
which we'll need to do at some point anyways, since this is the actual command that will
start the server.
And then it's here in this callback, as this is called, which is passed as a second
argument to the listen method, it's in here where I actually want to initialize my database.
So I'll cut the code from up here and instead put that in here.
And I'll still exit the server and this process if this fails.
But I want to do this after I started the server, so that if starting the server failed,
I don't even try to initialize the database.
Now here for the port, I'll also make some changes.
Instead of always using 3000, I'll add some alternative code, the one which is suggested
to me here now and which I can accept with a tab, like this, to take a look at the environment
variables that have been set for this app and use the value stored in a port environment
variable, if it's available, instead of 3000.
And actually for that, I'll invert this and use this value as a fallback value if this
should not be set.
Okay.
So that's that.
Therefore, I'll also hit tab again and accept these changes to this string where it outputs
the actual port if it's not the 3001.
And with that, I'm happy with this code here in app.js, at least for now.
We are initializing the database once the server started.
We're handling requests to slash users.
We're sending them to our user routes and in there, we're trying to invoke functions
the user's controller.js file, where then in turn, we try to use the create user function,
which is coming from the user model file, where we are indeed using the database to
insert users, for example.