Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
/ nongos Public archive

An opinionated framework for fast prototyping RESTful APIs with Node.js

License

Notifications You must be signed in to change notification settings

zonetti/nongos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nongos

JS Standard Style NPM version

Project still under development!

An opinionated framework for fast prototyping RESTful APIs with Node.js based on the MEAN stack!

Install

npm i nongos -S

Basic usage

const nongos = require('nongos')

nongos.resource(
  'users',
  {
    name: {
      type: String,
      required: [true, 'my custom message']
    },
    age: {
      type: Number,
      required: [true, 'required'],
      min: [10, 'should be at least 10']
    }
  }
)

nongos.start()

Done! Now you have a fully RESTful resource out of the box (CRUD). Give it a try:

GET http://localhost:1337/users
GET http://localhost:1337/users/:id
POST http://localhost:1337/users
PUT http://localhost:1337/users/:id
DELETE http://localhost:1337/users/:id

Configuration

Default configuration

{
  env: process.env.NODE_ENV ? process.env.NODE_ENV.trim() : 'development',
  port: process.env.PORT || 1337,
  db: {
    uri: 'mongodb://localhost',
    options: {
      useMongoClient: true
    }
  },
  logger: {
    transports: [
      new (winston.transports.Console)({
        colorize: true,
        timestamp: true
      })
    ]
  }
}

For more information on how to configure the path db.options, check the mongoose documentation.

For more information on how to configure the path logger, check the winston repository.

Changing the configuration

const nongos = require('nongos')

nongos.config({
  port: process.env.PORT || 3000,
  db: {
    uri: 'mongodb://myuser:[email protected]:59767/temp'
  }
})

// ...

Bootstrapping

If you want to override the default bootstrap you can use nongos.bootstrap(), just be aware that by doing so, the default bootstrap will no longer execute.

const nongos = require('nongos')

nongos.resource(
  'users',
  {
    name: String,
    age: Number
  }
)

nongos.bootstrap(() => {
  nongos.app.use(require('./my-middleware'))
})

nongos.start()

Resources

Validation

Because nongos uses mongoose you are able to validate your schema using the mongoose built-in rules or you can also create your own.

Check the full documentation at the mongoose website.

Methods

You can override or disable the default resource methods and also create new methods:

const nongos = require('nongos')
 
const {router, model} = nongos.resource(
  'users',
  {
    name: String,
    age: Number
  },
  {
    update: false,
    delete: false
  }
)

router.put('/:id', (req, res) => res.status(400).send({message: 'Nope!'}))

router.get('/foo', (req, res) => res.send('bar'))

router.post('/john-doe', (req, res, next) => {
  model.create({name: 'John Doe', age: 50}, (err, result) => {
    if (err) return next(err)
    res.send(result)
  })
})
 
nongos.start()

After these changes you will get:

GET http://localhost:1337/users // 200
GET http://localhost:1337/users/foo // 200 "bar"
GET http://localhost:1337/users/:id // 200
POST http://localhost:1337/users // 200
POST http://localhost:1337/users/john-doe // 200 {"__v": 0, "name": "John Doe", "age": 50, "_id": "5a3320b4e99a953ff07729a3"}
PUT http://localhost:1337/users/:id // 400 {"message": "Nope!"}
DELETE http://localhost:1337/users/:id // 404

Querying

You can use some advanced querying thanks to querymen.

For more information check its documentation.

GET http://localhost:1337/posts?page=2&limit=30
GET http://localhost:1337/posts?user=foobar&fields=title,content
GET http://localhost:1337/posts?limit=10&sort=-createdAt

Additional options

As seen above in the validation block, you can use the optional third parameter of nongos.resource() to override the default CRUD behavior.

In addition to that you can also configure the keywords feature powered by mongoose-keywords. Check its documentation for more information on how to do so.

const nongos = require('nongos')
 
const {router, model} = nongos.resource(
  'users',
  {
    name: String,
    age: Number
  },
  {
    list: true,
    create: true,
    read: true,
    update: false,
    delete: false,
    keywords: {
      path: ['name']
    }
  }
)

nongos.start()

Stack explained

License

MIT

About

An opinionated framework for fast prototyping RESTful APIs with Node.js

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published