5 minutes to build a Restful backend

Raw material

  • Express
  • Mongoose
  • Seven-express
  • Express-session

Start

  • Use NPM to install express and scaffolding $NPM install –save express -g express-generator
  • The default is to use Jade to initialize the project template, you can use –view== to specify other template. $express seven-test $CD seven-test $NPM install $NPM install mongoose seven-express express-session

Configure

App.js configuration

The default configuration is cumbersome, we first to streamline the configuration file, leaving only what we need, if you need other middleware can be configured.

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'))); (bodyParser.json) (app.use); app.use (bodyParser.urlencoded (extended: {false})); app.use (cookieParser ()); app.use (express.static (path.join (__dirname,'public')); module.exports = app);

Here we go for further configuration, we need to introduce the middleware, and configuration.

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 (mongoose.connect); / / database connection ('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 ('12345' name:'seven-test'{secret:, cookie:}, {maxAge: 80000, resave: false, saveUninitialized: true})); / / add seven middleware seven.creator (APP, router, path.join (__dirname,'schema'); module.exports = app);

Add schema

Add schema folder in the project directory, and then add a variety of mongoose schema file. If you want the seven rights management function, you will need to add a user table, and add username, password, role attributes.

Const mongoose = require ('mongoose'); let User = new mongoose.Schema (username: String / / / / {username, password password: String / role: String, the identity of the user, module.exports = User;});

Add seven configuration file

Custom configuration can refer to the introduction of the following seven middleware, here are examples.

{"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 get the whole project run.

$NPM start

If normal, will appear

Users Pagination get /user/page/: page post Delete delete /user/: ID Update /user/: ID Login /login Retrieve get /user/: Id put: Create /user

These are schema for the automatic mapping of the Restful API, with a configurable parameter check function, with configurable rights management functions.

Up to now we do not write a line of code to achieve the entire Restful API back-end function.

Seven-express

So how do we do this automated mapping?

Well, mainly from Mongoose.schema to Restful API mapping seven-express, and add the maturity of other functional modules, which I also recently in a middleware development desperately (or is now open source plug-ins), in GitHub, and released in npm.

Now there are more detailed Chinese documents, project address: Seven

Because I am now relatively heavy academic, so the speed of development 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 process, you can contact me directly, you can also direct maintenance, I will respond to you the first time, we are also welcome to this kind of non back-end mode of valuable suggestions.

Email: [email protected]