introduce

Keystone is an open source Node.js CMS and Web application platform based on Express and MongoDB.

Keystone says on its website that it is “the easiest way to build data-driven websites, applications, and apis in Node.js”.

Keystone comes with the following features:

  1. Express.js and MongoDB are built in

  2. Dynamic routing

  3. A useful database domain type

  4. Automatically generates an administrator interface

  5. Data model-based form processing

  6. Session management and authentication

I think the best thing about Keystone is that it automatically implements the background management interface for you, creating, managing, editing, deleting, etc., based on the model you define. It saves a lot of effort. Implementing a website like this is all about defining the model and writing front-end code.

This should be a front-end monkey outsourcing a sharp weapon.

Installation preparation

  1. Installation node. Js 0.10 +

  2. Install the mongo v2.4 +

  3. Install Yeoman NPM install -g Yo

  4. Install the Keystone generator NPM install -g generator- Keystone

start

  1. Create a project directory mkdir my-keystone

  2. Go to the project directory CD my-Keystone

  3. Generate code yo Keystone

The code generator will ask you questions such as the project name, whether to build in a blog, photo album, and contact form, whether to add a User model, and add a password for an administrator account

  1. The installation depends on NPM install

  2. Run the Node Keystone project

Open http://localhost:3000 in a browser to view open background management via http://localhost:3000/keystone

Model

var keystone = require('keystone'),
    Types = keystone.Field.Types;
 
var Post = new keystone.List('Post', {
    autokey: { path: 'slug', from: 'title', unique: true },
    map: { name: 'title' },
    defaultSort: '-createdAt'
});
 
Post.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft' },
    author: { type: Types.Relationship, ref: 'User' },
    createdAt: { type: Date, default: Date.now },
    publishedAt: Date,
    image: { type: Types.CloudinaryImage },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    }
});
 
Post.defaultColumns = 'title, state|20%, author, publishedAt|15%'
Post.register();Copy the code

This is an example given on the official website, a model of an article, with details

CloudinaryImage. You need to purchase CloudinaryImage. If you don’t want to use Cloudinary, Keystone also provides types.localfile so we can change image to:

image: { type: Types.LocalFile, dest: 'public/upload', 
        prefix: '/upload',
        format: function(item, file){
            return ''; }}Copy the code

If you want to use the CDN service, you can synchronize the images folder with the CDN and then change prefix to the prefix provided by your CDN provider.

Routing and View

Route configuration file: routes/index.js Middleware: routes/middleware.js

var keystone = require('keystone'), middleware = require('./middleware'), importRoutes = keystone.importer(__dirname); / / used middleware keystone. Pre (' routes' middleware. InitErrorHandlers); keystone.pre('routes', middleware.initLocals); keystone.pre('render', middleware.flashMessages); Keystone.set ('404', function(req, res, next) {res.notfound(); }); Keystone.set ('500', function(err, req, res, next) {var title, message; if (err instanceof Error) { message = err.message; err = err.stack; } res.err(err, title, message); }); Var routes = {views: importRoutes('./views')}; Exports = module.exports = function(app) {app.get('/', routes.views.index); // exports = module.exports = function(app) {app.get('/', routes.views.index); // Before routing, do anything with middleware app.get('/protected', middleware.requireuser, middleware.other, routes.views.protected); }Copy the code

configuration

Keystone provides many configuration items. You can configure them based on your requirements. For more configuration items supported by Keystone, see Configuration Guide.

The configuration file to be modified is keystone.js

The project structure

| | - lib | custom libraries and code - models database model | | program - public | public static files (CSS, js, images, etc.) | - routes | | - API | | application API controller | | - views | View controller | programs | | -- index. Js | | initialization program routing and view | | -- middleware. Js | | | for routing to customize middleware - templates | | - includes | | general. Jade Component here | | - layouts | | basis. The jade layout here | | - mixins | | general. Jade mixins here | | - views | | view template program | - updates | data assembly and migration script | -- package. Json | | to NPM project configuration -- keystone. Js | initiator's main scriptCopy the code

disadvantages

Keystone powerful automatic management background, but also to the place where I use restrictions, support basic functions such as delete, upload files, but lose the flexibility in some business custom, may be I don’t study, the official did not give the background management custom document, the subsequent I will study how the depth of customization management background, Share them again if possible.

reference

English document: keystonejs.com/zh/docs/ Project Address: github.com/keystonejs/…