├── .gitignore
├── README.md
├── express.js
├── express.test.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | .idea
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > Node.js REST API Example with MongoDB, Mongoskin, Express 3 and 4
2 |
3 | # Express.js 4.x
4 |
5 | `master` branch
6 |
7 | Full tutorial:
8 |
9 | Brief instructions:
10 |
11 | ```
12 | $ git clone https://github.com/azat-co/rest-api-express.git
13 | $ cd rest-api-express
14 | $ npm install
15 | $ node express.js
16 | ```
17 |
18 | In a new terminal window:
19 |
20 | ```
21 | $ mocha express.test.js
22 | ```
23 |
24 | Or, if you don't have mocha installed globally:
25 |
26 | ```
27 | $ ./node_modules/mocha/bin/mocha express.test.js
28 | ```
29 |
30 | You can use `package.json` commands:
31 |
32 | ```
33 | $ npm start
34 | $ npm test
35 | ```
36 |
37 | ---
38 |
39 | # Express.js 3.x
40 |
41 | `express3` branch
42 |
43 | Full tutorial:
44 |
45 | Brief instructions:
46 |
47 | ```
48 | $ git clone https://github.com/azat-co/rest-api-express.git
49 | $ cd rest-api-express
50 | $ git checkout express3
51 | $ npm install
52 | $ node express.js
53 | ```
54 |
55 | in a new window
56 |
57 | ```
58 | $ mocha express.test.js
59 | ```
60 |
61 | or (if you don't have mocha installed globally):
62 |
63 | ```
64 | $ ./node_modules/mocha/bin/mocha express.test.js
65 | ```
66 |
--------------------------------------------------------------------------------
/express.js:
--------------------------------------------------------------------------------
1 | var express = require('express'),
2 | mongoskin = require('mongoskin'),
3 | bodyParser = require('body-parser')
4 | logger = require('morgan')
5 |
6 | var app = express()
7 | app.use(bodyParser.json())
8 | app.use(bodyParser.urlencoded({extended: true}))
9 | app.use(logger('dev'))
10 |
11 | var db = mongoskin.db('mongodb://@localhost:27017/test', {safe:true})
12 |
13 | app.param('collectionName', function(req, res, next, collectionName){
14 | req.collection = db.collection(collectionName)
15 | return next()
16 | })
17 |
18 | app.get('/', function(req, res, next) {
19 | res.send('please select a collection, e.g., /collections/messages')
20 | })
21 |
22 | app.get('/collections/:collectionName', function(req, res, next) {
23 | req.collection.find({} ,{limit: 10, sort: {'_id': -1}}).toArray(function(e, results){
24 | if (e) return next(e)
25 | res.send(results)
26 | })
27 | })
28 |
29 | app.post('/collections/:collectionName', function(req, res, next) {
30 | req.collection.insert(req.body, {}, function(e, results){
31 | if (e) return next(e)
32 | res.send(results)
33 | })
34 | })
35 |
36 | app.get('/collections/:collectionName/:id', function(req, res, next) {
37 | req.collection.findById(req.params.id, function(e, result){
38 | if (e) return next(e)
39 | res.send(result)
40 | })
41 | })
42 |
43 | app.put('/collections/:collectionName/:id', function(req, res, next) {
44 | req.collection.updateById(req.params.id, {$set: req.body}, {safe: true, multi: false}, function(e, result){
45 | if (e) return next(e)
46 | res.send((result === 1) ? {msg:'success'} : {msg: 'error'})
47 | })
48 | })
49 |
50 | app.delete('/collections/:collectionName/:id', function(req, res, next) {
51 | req.collection.removeById(req.params.id, function(e, result){
52 | if (e) return next(e)
53 | res.send((result === 1)?{msg: 'success'} : {msg: 'error'})
54 | })
55 | })
56 |
57 | app.listen(3000, function(){
58 | console.log('Express server listening on port 3000')
59 | })
60 |
61 |
--------------------------------------------------------------------------------
/express.test.js:
--------------------------------------------------------------------------------
1 | var superagent = require('superagent')
2 | var expect = require('expect.js')
3 |
4 | describe('express rest api server', function(){
5 | var id
6 |
7 | it('posts an object', function(done){
8 | superagent.post('http://localhost:3000/collections/test')
9 | .send({ name: 'John'
10 | , email: 'john@rpjs.co'
11 | })
12 | .end(function(e, res){
13 | // console.log(res.body)
14 | expect(e).to.eql(null)
15 | expect(res.body.length).to.eql(1)
16 | expect(res.body[0]._id.length).to.eql(24)
17 | id = res.body[0]._id
18 | done()
19 | })
20 | })
21 |
22 | it('retrieves an object', function(done){
23 | superagent.get('http://localhost:3000/collections/test/'+id)
24 | .end(function(e, res){
25 | // console.log(res.body)
26 | expect(e).to.eql(null)
27 | expect(typeof res.body).to.eql('object')
28 | expect(res.body._id.length).to.eql(24)
29 | expect(res.body._id).to.eql(id)
30 | expect(res.body.name).to.eql('John')
31 | done()
32 | })
33 | })
34 |
35 | it('retrieves a collection', function(done){
36 | superagent.get('http://localhost:3000/collections/test')
37 | .end(function(e, res){
38 | // console.log(res.body)
39 | expect(e).to.eql(null)
40 | expect(res.body.length).to.be.above(0)
41 | expect(res.body.map(function (item){return item._id})).to.contain(id)
42 | done()
43 | })
44 | })
45 |
46 | it('updates an object', function(done){
47 | superagent.put('http://localhost:3000/collections/test/'+id)
48 | .send({name: 'Peter'
49 | , email: 'peter@yahoo.com'})
50 | .end(function(e, res){
51 | // console.log(res.body)
52 | expect(e).to.eql(null)
53 | expect(typeof res.body).to.eql('object')
54 | expect(res.body.msg).to.eql('success')
55 | done()
56 | })
57 | })
58 |
59 | it('checks an updated object', function(done){
60 | superagent.get('http://localhost:3000/collections/test/'+id)
61 | .end(function(e, res){
62 | // console.log(res.body)
63 | expect(e).to.eql(null)
64 | expect(typeof res.body).to.eql('object')
65 | expect(res.body._id.length).to.eql(24)
66 | expect(res.body._id).to.eql(id)
67 | expect(res.body.name).to.eql('Peter')
68 | done()
69 | })
70 | })
71 | it('removes an object', function(done){
72 | superagent.del('http://localhost:3000/collections/test/'+id)
73 | .end(function(e, res){
74 | // console.log(res.body)
75 | expect(e).to.eql(null)
76 | expect(typeof res.body).to.eql('object')
77 | expect(res.body.msg).to.eql('success')
78 | done()
79 | })
80 | })
81 | it('checks an removed object', function(done){
82 | superagent.get('http://localhost:3000/collections/test/')
83 | .end(function(e, res){
84 | // console.log(res.body)
85 | expect(e).to.eql(null)
86 | expect(res.body.map(function (item){return item._id})).to.not.be(id)
87 | done()
88 | })
89 | })
90 | })
91 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "rest-api-express",
3 | "version": "0.0.4",
4 | "description": "",
5 | "main": "express.js",
6 | "scripts": {
7 | "start": "node express.js",
8 | "test": "./node_modules/mocha/bin/mocha express.test.js"
9 | },
10 | "repository": {
11 | "type": "git",
12 | "url": "https://github.com/azat-co/rest-api-express.git"
13 | },
14 | "author": "Azat Mardan",
15 | "license": "BSD-2-Clause",
16 | "bugs": {
17 | "url": "https://github.com/azat-co/rest-api-express/issues"
18 | },
19 | "dependencies": {
20 | "body-parser": "1.9.2",
21 | "express": "4.10.1",
22 | "mongoskin": "1.4.4",
23 | "morgan": "1.5.0"
24 | },
25 | "devDependencies": {
26 | "expect.js": "0.3.1",
27 | "mocha": "2.0.1",
28 | "superagent": "0.20.0"
29 | }
30 | }
31 |
--------------------------------------------------------------------------------