Build A Blog Platform With MongoDB and Express- 10 mins
There are also some document here for the initial version of this demo where I used the native mongodb node.js driver and didn’t integrate with Passport.js.
MongoDB is definitely another topic to discuss, for the brevity of this post, I’ll assume that you’ve already have MongoDB properly installed, and you’ve started the connection here.
In the initial version I was using the native MongoDB Node.js driver, it worked fine until I tried to integrate with Passport.js like this:
The error message is db connection already exists, so I checked my db files, taking user.js as an example:
As you can see, for each data-manipulating method, I would open the mongodb at first, then close the db after query or save. In passport.js, It seems that I was querying and serializing some user data, and the db didn’t have the chance to close before the next open, because when I remove either the serialize code or the query code, the db is already in use error is gone. Go back to user.js, It seems that whenever I open and close db, the
mongodb.close() is in the callback function. So I would say that some callback functions were not called before the code went to implementing the next method.
After googling for a while, it seems that the way of opening and closing db frequently is not quite ideal/convenient, so I switched to mongoose(will address this later).
If you use the
express-generator, you’ll see the boilerplate of this entry point file looks similar to this:
Basically, what we do in this file including starting the app with
var app = express();, making sure those essential middlewares are used, such as the routers, bodyParser, also setup things like the view engine, views, port, in addition to the express generator app.js, we also need to connect to the database at the beginning.
As far as I’m concerned, user management is always interesting, there are many ways of dealing user authentication, the bottom line is, find the one fits your requirement best. The very basic implementation would be asking user to provide information of username, email, password etc and just save them into db, and use one of those fields as the unique identifier for querying. However, if the authentication system supports OAuth verification(with passport.js), you need to consider how to save the user data. Initially I tried to save things like in case I need to associate social media accounts:
But it turned out I don’t need to associate accounts after all, then this structure gave me a hard time for trying to save user info so that I can easily query them later. So I changed the structure as a unified user Schema:
That means if there is some extra info provided, I’ll save them into DB, else just leave them blank. Some may say, this kind of thing is much easier to understand or implement in sql database. But generally I’d like to stick to mongodb this time and not mixing sql and nosql for now.
Session and Flash
Before I get to know the web dev framework, I didn’t realize how essential the session could be. It is quite useful to keep one user logged in between pages, so we can use this piece of information to show related information or keep track of the user somehow. Beyond that, there are many package built on ‘session’, such as the
connect-flash, it brings some information to the redirected url.
File upload is also very interesting, because you have to consider your business logic/requirement before decide which plan to use. Usually people would save them into some folders on the server, and distribute those files into different servers depends on the amount of those files. Additionally, you can use cloud services like Amazon cloudfront to either transfer file on DB to cloud or transfer files directly.
Besides, there used to be a file upload part in Express 3.x, but it was removed in Express 4.x, but there are some nice middlewares such as formiddable or busboy to do this job well.
Once you finished local development, you may want to deploy your project. The traditional way is to install node and mongo on Ubuntu or CentOS(Ubuntu is better I think), and scp the project file into your server. Honestly, I think if you don’t want to spend too much time on those OP stuff, There is nothing wrong using cloud hosting platforms like
Heroku, they are much easier to setup and all provide some kind of free trial storage.
I’ve learned a lot from this little demo project(although I haven’t spend time on the front end code yet). But still a long way to go to make it a ‘real’ demo.