A blog system written on the Sails.js framework, using MongoDB for document storage.
This is a demonstrative example of a web application built using the Sails.js framework with MongoDB. Public-facing views are rendered via EJS templates on the server. The administration UI is powered by Angular.js.
- If using Sails.js for the first time, be sure to
npm install -g sails
. - Clone/fork the Git repository to your machine.
- Run
npm install
inside the directory. - Create a MongoDB database for your blog (e.g.
sails-blog
). - Create a file called
local.js
in theconfig/
directory:
'use strict';
module.exports = {
connections: {
'sails-blog': {
adapter: 'sails-mongo',
host: 'localhost', // Host if remote connection
port: 27017, // Port if not default
// Uncomment these and add values if you need to authenticate to MongoDB
// user: '',
// password: '',
database: 'sails-blog' // The name of your database
}
};
- You will need to create an initial author. Run
sails console
. - Enter
Author.create({ email: '[email protected]', password: 'yourPassword', displayName: 'Your Name' }).exec(sails.log);
(with your information) - Observe the ID of the newly created author.
- Open
config/sailsBlog.js
and set the value ofadminAuthorId
to be the ID of your newly created author to give yourself administrative access. - Edit any other values in
sailsBlog.js
as you see fit.
Run the blog at any time with sails console
for a REPL or sails lift
for just output. Make sure mongod
is running. Press Ctrl + C
to stop.
Templates for the frontend views are rendered with EJS. They are in the views/
directory. Subdirectories include layouts/
, which contains full-document templates, and components/
, which are reusable template partials. All views are wrapped in the frontend.ejs
layout. View assets (JavaScript, CSS, images, etc.) are contained in the assets/
directory.
Each of the static public-facing views are rendered by the ViewsController
, which renders the EJS template and passes the relevant data into it. To add or manipulate the data passed into a view, edit api/controllers/ViewsController.js
.
Additional frontend views can be created by adding additional methods to the ViewsController
, creating a view file in views/
, and adding another route to the ViewController
method routes in config/routes.js
.