Raw material
express
mongoose
seven-express
express-session
Start
-
Using npm to install express and scaffolding
$ npm install -g --save express express-generator
-
Initialization project
The default is to use the Jade template, and you can use -- view=== to specify other templates.
$ express seven-test $ cd seven-test $ npm install $ npm install mongoose seven-express express-session
Configure
app.js configuration
The default configuration is tedious. Let's first simplify the configuration file, leaving only the parts we need. If you need other middleware, you can configure it later.
var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); module.exports = app;
Now let's go further and configure the middleware we need.
var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var session = require('express-session'); var mongoose = require('mongoose'); var seven = require('seven-express'); var router = express.Router(); var app = express(); // Connect to the database mongoose.connect('mongodb://localhost:27017/Test'); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); // Add session Middleware app.use(session({ secret: '12345', name: 'seven-test', cookie: {maxAge: 80000 }, resave: false, saveUninitialized: true })); // Adding seven Middleware seven.creator(app, router, path.join(__dirname, 'schema')); module.exports = app;
Add schema
Add the schema folder to the project directory, and then add various mongoose schema files to it. If you need the permission management function of seven, you need to add the user table by default, and add the username, password, role attributes.
const mongoose = require('mongoose'); let User = new mongoose.Schema({ // User name username: String, // Password password: String, // User identity role: String, }); module.exports = User;
Add the seven configuration file
Custom configurations can refer to the introduction of seven middleware later, and examples are given here.
{ "rule": { "user" : { "Create": { "key": "username", "bodyList": ["username", "role"] }, "Update": { "bodyList": ["username"] } } }, "authority": { "role": ["admin", "user", "superadmin"], "filter": { "user": { "Create": false, "Retrieve": ["admin"], "Update": ["user", "admin", "superadmin"] } } } }
Run
Now you can run the whole project.
$ npm start
If normal, it will appear.
users Pagination get /user/page/:page Create post /user Delete delete /user/:id Update put /user/:id Login post /login Retrieve get /user/:id
These are Restful API s automatically mapped from schema, which have configurable parameter checking function and configurable authority management function.
So far, without writing a single line of code, we have implemented the back-end functionality of the entire Restful API.
Seven-express
So how on earth do we achieve such automated mapping?
Well, it's mainly the mapping from Mongoose.schema to Restful API implemented by seven-express ion, and the addition of other mature functional modules. This is also a middleware (or plug-in) I've been working hard to develop recently. It's now open source on github and distributed in npm.
Now there are more detailed Chinese documents, project address: seven
Because I am a heavy student now, so the development speed will not be particularly fast, but I will do my best to maintain this open source project, welcome to use star. If there are any problems in the use process, you can contact me directly, or you can directly start maintenance, I will respond to you at the first time, and welcome you to make valuable suggestions on this no-back-end model.
E-mail: 610347922@qq.com