The Restful style API middleware built by Node relies on Express and Mongoose

preface

npm install "ltz-rest" --save
Copy the code

Rely on mongoose registration model, two sentence implementation of REST API

//1. Introduce framework const rest = require('ltz-rest');
app.use('/', rest);

Copy the code
  • Middleware automatic generation: restful convention style API
URL HTTP function
/tab POST Create an object
/tab GET The query object
/tab/objectId GET Access to the object
/tab/objectId PUT Update the object
/tab/objectId DELETE Delete the object

I. Use of details

'use strict';

const mongoose = require('mongoose');
const db = mongoose.connect('mongo: / / 127.0.0.1:27017 / test');

const express = require('express'); const app = express(); //1. Introduce framework const rest = require('ltz-rest');
app.use('/', rest);

app.listen(3000);


const Schema = mongoose.Schema;

const student = {
    name: String,
    age: Number,
    sex: String,
    xuexiao: {
        type: Schema.Types.ObjectId,
        ref: 'school'}, score: { shuxue: Number, yuwen: Number } }; // Register mongoose. Model ('student', new Schema(student)); const school = { title: String }; // Register mongoose. Model ('school', new Schema(school));

Copy the code

We have two registered models of Student School, both of which automatically generate apis. This plugin relies on Mongoose and will automatically generate the API only if you register with Mongoose.model ()

Student’s five API uses are generated automatically below

1:POST creates data

{http://localhost:3000/student body contains the JSON data"age": 5,"xuexiao":"58e8448918493009b7449229"."name":"Small four."."score": {"shuxue": 89,"yuwen": 77}."sex":"woman"} return data {"__v": 0."age": 5,
  "xuexiao": "58e8448918493009b7449229"."name": "Small four."."sex": "woman"."_id": "592a33db1b84a41131db2f56"."score": {
    "shuxue": 89,
    "yuwen"Note POST, the body of the request must be in JSON format, and the CONTent-type of the HTTP header needs to be set to Application/JSONCopy the code

2:GET query, all special keywords start with “_”

Query student document, the default _limit = 100 http://localhost:3000/student query student documentation, limit a and skip the jump: _limit=1&_skip=1 http://localhost:3000/student? Select * from student where _limit=1 and _skip=1; select * from student where _limit=1; _gt _lt _gte _lte _ne http://localhost:3000/student? _where={"score.shuxue": {"_gt"} //score. Shuxue > 90;"_regex":"^" li."_regex":"Li"."_regex":"$" li, names beginning with lee, lee in a name, name is li http://localhost:3000/student? at the end _where={"name": {"_regex":"^" li}} // Start with the name"Li"Query student document, join table display. Also said xuexiao query out not id, http://localhost:3000/student? query is the corresponding document Populate = populate (score. Shuxue > 90 and name = "Li") http://localhost:3000/student?_where={"score.shuxue": {"_gt": 90}."name": {"_regex":"Li"}}&_populate=xuexiao&_limit=1

Copy the code
Special keywords _ function demo
_limit limit _limit=10, returns a limit of 10 data
_skip skip _skip=10, skip 10 data
_gt, _LT, _GTE, _LTE, _NE Limit numbers: greater than, less than, greater than or equal to, less than or equal to, not equal to > 10. _ne=20
_regex Character string Query based on regular Expression {“_regex”:”^ lee “}, name begins with “lee”
_populate Return table data _populate=xuexiao. Local and populate with xuexiao data

3:GET Queries based on the unique ID

Query student document, the only id 592 a33bf1b84a41131db2f55, if need school data _populate http://localhost:3000/student/592a33bf1b84a41131db2f55? _populate=xuexiaoCopy the code

4:PUT Updates data based on the unique ID

Example Modify data PUT to change the age to 18. Populate gets school information using _populate. {http://localhost:3000/student/592a33bf1b84a41131db2f55?_populate=xuexiao body contains the JSON data"age":18} Return data {"_id": "592a33bf1b84a41131db2f55"."age": 18."xuexiao": {
    "_id": "58ddd5db6216a905ce973de4"."name": "No. 1 High School"."__v": 0}."name": "Little red 2222"."sex": "woman"."__v": 0."score": {
    "shuxue": 99,
    "yuwen"Note PUT, the body of the request must be in JSON format, and the CONTent-type of the HTTP header must be set to Application/JSONCopy the code

5:DELETE Deletes data based on the unique ID

DELETE to DELETE the document id is 592 a33bf1b84a41131db2f55 return data {http://localhost:3000/student/592a33bf1b84a41131db2f55"_id": "592a33bf1b84a41131db2f55"."age": 18."xuexiao": "58ddd5db6216a905ce973de4"."name": "Little red 2222"."sex": "woman"."__v": 0."score": {
    "shuxue": 99,
    "yuwen": 88}}Copy the code

Skip the middleware

This request is not executed by the middleware, and next is called to execute the next middleware.

/ / ALL: skip ALL the API request / / GET: skip the GET request / / 5 API: GET, GETID, PUT, POST, DELETE, you can choose to skip. rest.skip = {'student':'ALL'// Skip all student apis'school': 'GET,GETID'// Skip school GET and GETID, other PUT,POST,DELETE returns normal results. Failed to perform http://localhost:3000/school} to GET access to school document, Because the skip GET DELETE school document DELETE http://localhost:3000/school/592a33bf1b84a41131db2f55 implementation success, because there is no skip the DELETE.Copy the code

1.0 implement functions, configure whether to skip middleware.

0.9 Write documentation and publish NPM

0.8 implementation Population

0.7 Querying a Port You can query numbers greater than, less than, equal to, and unequal. gt lt gte lte ne

0.6 Querying port Implementation Character String Query based on regular expression. _where = {name: {” _regex “:” ^ “li}}

0.5 Summary of ideas, code design, interface design, write some demos, read documents.

0.4 Error stack throws forward.

0.3 Query interface to achieve sorting.

Version 0.2 has implemented 5 interfaces, query interface only implemented SKIP and limit.

0.1 version of the framework, debugging interface.

  • Project GitHub:https://github.com/liangtongzhuo/ltz-rest
  • Personal blog: http://liangtongzhuo.com
  • Visual interface may be added later.