Now, refactoring the routes
is only a start though.
There's more we can refactor.
And there is more we can refactor in those route files,
but before we do that,
I'll take a look at the app.js file.
This file is definitely not bad.
It's pretty lean, but there are a couple of things
we could extract.
For example, you could extract configuration settings
like this object for the session
into a separate config.file.
It's not a must do.
None of the things mentioned here are must dos.
But it is something you could do,
if you find yourself having a lot of configuration objects
in that file, and you wanna split that up.
And therefore, to show this to you, I will do it here.
But again, keep in mind, it's optional.
Splitting the routes was also optional,
but strongly recommended.
Extracting configuration objects is optional
and not necessarily recommended.
It's totally up to you.
So we'll add a new folder and I'll name it config.
Though, the name is up to you.
This is no reserved name or anything like that.
You can name that folder whatever you want.
And here in this config folder,
I will add a session.js file like this.
And in there I'll now register
all these session config objects that I use in app.js.
Like this object for configuring the MongoDBStore
and then this object for configuring the session itself.
And actually, I'll take this entire sessionStore
constant here from app.js and move that into session.js
like this.
And therefore, I'll also grab the MongoDBStore here
and cut that from app.js and bring that into session.js
because we're now using that MongoDBStore there.
Now, I need that MongoDBStore function here.
And for this in app.js, I'll extract
this require statement here, where I require MongoDBStore.
And I will cut that, and bring that here
into session.js as well.
And I will now wrap this sessionStore creation
into a function,
createSessionStore would be a fitting name
and I'll move that into this function.
I will also move this line here into this function
because this is not an import.
I'm just creating the MongoDBStore object.
And since this needs that session middleware,
which we'll be using,
I will simply accept this here as a parameter value
in my custom function.
And I will then export this function
with module.exports as you learned it.
So I will export this function here.
I'll name it createSessionStore
and point at this createSessionStore function
I just created like this.
And now back in app.js,
we can import from this config.file,
call this function and pass the session to it,
to have this logic outsourced.
In this function, I now just also want to return
my sessionStore here, which I'm creating.
So that the logic for creating it is outsourced
and these configuration items
are, but I can still get my session store in the place
where I need it, which is the app.js file.
So here I'll get my sessionConfig now,
let's say in app.js by requiring ./config/session.
So now I'm requiring this object,
which I'm exporting here in the session.js file.
And in there, I'll have the createSessionStore function,
which I can call.
So therefore, I can now get my mongoDBSessionStore
by executing sessionConfig.createSessionStore.
And to this function, I need to pass session,
this middleware, which I'm importing here
because that is used by that function.
And now I'll just also grab this
session creation here or just that object.
It is up to you what you wanna do.
I'll grab just the object here
to show you that you can do both.
And I'll add this here in session.js as well.
So here I'll add another function,
createSession or a createSessionConfig.
And there, I will simply return this object,
which I just cut.
There, I need my sessionStore, so I'll except this here
as a parameter as well.
And then I'll export this unction
just as I'm exporting createSessionStore.
So this createSessionConfig function
will be exported as well.
And now in app.js, we can also call that.
So now here, where I need the config,
I can use my sessionConfig
and call createSessionConfig and pass my mongoDBSessionStore
as an argument, as a parameter value to this function.
So that's this mongoDBSessionsStore I created before
with that other function that lives
in the sessionConfig file.
And again, I will emphasize, this is optional
and this is all just one way of implementing this.
We could do the entire session setup into session.js file
and directly use the store which we are creating here
in the config and then just return
the initialized session middleware,
so you can do whatever you want.
This is just one possible approach
to extract some parts that are involved
with this configuration from the app.js file
into a separate file.
And we could do this for the configuration
for other middleware as well, but I'll keep it like this.
I'll just focus on the session at the moment
and just saved this and see whether that still works.
If I'm quickly logging in here,
yeah, I can still move around here.
So that all seems to work.
Let's see if flashing error message
has onto the session also still works
if I try to create a user that already exists, yes it does.
So that's how we could extract configuration
if we want it to do that.